<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;

class UserController extends CommonController {
	function _initialize(){
		parent::_initialize();
        vendor('Weixinpay.WxPayPubHelper');
        vendor('Weixinpay.wechatH5Pay');
		$perv_url=$_SERVER['HTTP_REFERER'];
		if(!$perv_url){
			$perv_url="/";
		}
		if(!strpos($perv_url,'register')&&!strpos($perv_url,'login')){
			session('perv_url',$perv_url);
		}
        $mode=I('get.mode');
        $token=I('get.token');
        $uidString=$token.'uid';
        $redis =connectRedis();
        $uid=$redis->get($uidString);
        if($mode){
            session('mode',$mode);
        }
        if($uid) {
            session('user_id',$uid);
        }
		$user_id=session('user_id');
		if($user_id){
			//登录时不允许访问操作
			$agreen_action=array('login','register','login_in','register_in','register_validate','sms_captcha','forget_pwd','verify','forget_pwd_moblie','forget_pwd_yanzheng','forget_pwd_rest');
			$users=M('users');
			$userinfo=$users->find($user_id);
			if($userinfo['username']==''){
				$userinfo['username']=$userinfo['phone'];
			}
			$this->assign('userinfo',$userinfo);
			$action_name=ACTION_NAME;
			if(in_array($action_name,$agreen_action)){
				$this->redirect('user/center');
			}
		}else{
			//未登录时允许访问操作
			$agreen_action=array('help_info','ajax_help_list','help_list','help','app_qq_login','app_weibo_login','qqlogin','wx_app_login','qq_app_login','wb_app_login','center','qq_login','weibo','wx_qjbd','weixin_return','register_validate2','sms_captcha2','weixin_login','collect','login','register','login_in','register_in','login_out','register_validate','sms_captcha','forget_pwd','verify','forget_pwd_moblie','forget_pwd_yanzheng','forget_pwd_rest','jiemi');
			$action_name=ACTION_NAME;
			if(!in_array($action_name,$agreen_action)){
				$this->redirect('user/login');
			}
		}
	}
    public function index(){
		$this->display();
	}
	public function login(){
		$perv_url=$_SERVER['HTTP_REFERER'];
		if(!strpos($perv_url,'register')&&!strpos($perv_url,'login')){
			session('perv_url',$perv_url);
		}
		session('step','1');
		$this->display();
	}
	public function register(){
	
		$perv_url=$_SERVER['HTTP_REFERER'];
		if(!strpos($perv_url,'register')&&!strpos($perv_url,'login')){
			session('perv_url',$perv_url);
		}
		$this->display();
	}
	
	public function login_in(){
		if($_POST){
			$users=M('users');
                $data=I('post.');
                $phone=$data['phone'];
                $password=$data['password'];
                $userinfo=$users->where('phone="'.$phone.'"')->find();
                if($userinfo){
				$pass=passport_decrypt($userinfo['password'],'wnsc');
				if($pass==$password){
					
					
					session('user_id',$userinfo['id']);
					session('user_name',$userinfo['username']);
					session('user_pic',$userinfo['user_pic']);
					session('login_time',$userinfo['login_time']);
					//更新登录时间
					$date_time=date('Y-m-d H:i:s', time());
					$login_info['id']=$userinfo['id'];
					$login_info['login_time']=$date_time;
					$users->save($login_info);
					
					if($data['rememberMe']==1){
						cookie('user_id',$userinfo['id'],3600*24*7);
           				cookie('user_name',$userinfo['username'],3600*24*7);
					}
					$perv_url=session('perv_url');
					if(!$perv_url){
						$perv_url='/user/center';
					}
					//添加cookie 购物车
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						addcart($userinfo['id'],$v['shop_id'],$v['sku_id'],$v['num']);
					}
					cookie('cart_array',null);
					session('zd_out',null);
					$this->ajax_r(1,$perv_url,'登录成功！');
				}else{
					$this->ajax_r(0,$password,'密码错误！');
				}
			}else{
				$rs=wunen_register($phone,$password);
				$rs=json_decode($rs,true);
				if($rs['success']){
					$info['username']=$phone;
					$info['phone']=$phone;
					$info['password']=passport_encrypt($password,'wnsc');
					$info['addtime']=time();
					$date_time=date('Y-m-d H:i:s', time());
					$info['login_time']=$date_time;
					$rs=$users->add($info);
					$userinfo=$users->where('phone="'.$phone.'"')->find();
					session('user_id',$userinfo['id']);
					session('user_name',$userinfo['username']);
					session('user_pic',$userinfo['user_pic']);
					session('login_time',$userinfo['login_time']);
					//更新登录时间
					$date_time=date('Y-m-d H:i:s', time());
					$login_info['id']=$userinfo['id'];
					$login_info['login_time']=$date_time;
					$users->save($login_info);
					if($data['rememberMe']==1){
						cookie('user_id',$userinfo['id'],3600*24*7);
           				cookie('user_name',$userinfo['username'],3600*24*7);
					}
					$perv_url=session('perv_url');
					if(!$perv_url){
						$perv_url='/user/center';
					}
					//添加cookie 购物车
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						addcart($userinfo['id'],$v['shop_id'],$v['sku_id'],$v['num']);
					}
					cookie('cart_array',null);
					session('zd_out',null);
					$this->ajax_r(1,$perv_url,'登录成功！');
				}else{
					$this->ajax_r(0,'','该用户不存在！');
				}
				$this->ajax_r(0,'','该用户不存在！');
			}
			
			
		}
	}
	
	public function register_in(){
		if($_POST){
			$users=M('users');
			$data=I('post.');
			$moblie=$data['mobile'];
			$password=$data['password'];
			$sms_captcha=$data['sms_captcha'];
			$code=session('moblie_code');
			
			if($code){
				if($code==$sms_captcha){
					if(session('tj_userid')){
						$info['tj_userid']=session('tj_userid');
					}else{
						$info['tj_userid']=0;
					}
					$info['username']=$moblie;
					$info['phone']=$moblie;
					$info['password']=passport_encrypt($password,'wnsc');
					$info['addtime']=time();
					$date_time=date('Y-m-d H:i:s', time());
					$info['login_time']=$date_time;
					$rs=$users->add($info);
					if($rs){
						session('tj_userid',null);
						session('user_id',$rs);
						session('user_name',$info['username']);
						$perv_url=session('perv_url');
						if(!$perv_url){
							$perv_url='/user/center';
						}
						$this->ajax_r(1,$perv_url,'注册成功');
					}
				}else{
					$this->ajax_r(0,'','短信验证码错误');
				}
			}else{
				$this->ajax_r(0,'','短信验证码失效');
			}
			
		}
	}
	public function login_out(){
		session('user_id',null);
		session('user_name',null);
		cookie('user_id',null);
		cookie('user_name',null);
		session('zd_out','1');
		echo "<script language='javascript' type='text/javascript'>";
		echo "window.location.href='/'";
		echo "</script>";
		
	}
	
	public function weixin_login(){
		session('zd_out',null);
		$this->redirect('user/center');
	}
	public function register_validate(){
		$data=I('get.');
		$users=M('users');
		$moblie=$data['mobile'];
		$rs=$users->where('phone="'.$moblie.'" and ishidden=1')->find();
		if($rs){
			$this->ajax_r(1,'','该帐号已注册');
		}else{
			$this->ajax_r(0,'','');
		}
		
	}
	public function register_validate2(){
		$data=I('get.');
		$users=M('users');
		$moblie=$data['mobile'];
		$rs=$users->where('phone="'.$moblie.'" and ishidden=1')->find();
		if($rs){
			$this->ajax_r(1,'','该帐号已注册');
		}else{
			$this->ajax_r(0,'','');
		}
		
	}
	public function sms_captcha(){
		$data=I('post.');
		$moblie=$data['send'];
		$code=rand(1000,9999);
		$rs=duanxin($code,$moblie);
		if($rs['resultcode']==0){
			session('moblie_code',$code);
			$this->ajax_r(1,'','发送成功！');
		}else{
			$this->ajax_r(0,$rs['errordescription'],'发送失败！');
		}
	}
	public function sms_captcha2(){
		$data=I('post.');
		$moblie=$data['send'];
		$code=rand(1000,9999);
		$rs=duanxin($code,$moblie);
		if($rs['resultcode']==0){
			session('bd_moblie_code',$code);
			$this->ajax_r(1,'','发送成功！');
		}else{
			$this->ajax_r(0,$rs['errordescription'],'发送失败！');
		}
	}
	public function forget_pwd(){
		if($_POST){
			$data=I('post.');
			$users=M('users');
			$rs=$users->where('phone="'.$data['phone'].'" and ishidden=1')->find();
			if($rs){
				session('forget_user',$data['phone']);
				session('step','2');
				$this->ajax_r(1,'','验证成功！');
			}else{
				session('step','1');
				$this->ajax_r(0,'','用户不存在！');
			}
			
		}else{
			$step=$_GET['step']?$_GET['step']:1;
			if(session('step')!=$step&&session('step')){
				$this->redirect('user/forget_pwd',array('step'=>session('step')));
			}
			$this->display();
		}
		
	}
	public function forget_pwd_moblie(){
		$moblie=session('forget_user');
		if($moblie){
			$code=rand(1000,9999);
			$rs=duanxin($code,$moblie);
			if($rs['resultcode']==0){
				session('forget_pwd_code',$code);
				$this->ajax_r(1,'','发送成功！');
			}else{
				$this->ajax_r(0,$rs['errordescription'],'发送失败！');
			}
		}else{
			session('step','1');
			$this->ajax_r(0,'','验证手机不存在,请重新验证！');
		}
		
	}
	public function forget_pwd_yanzheng(){
		if($_POST){
			$moblie=session('forget_user');
			if($moblie){
				$sms_captcha=I('post.sms_captcha');
				if($sms_captcha==session('forget_pwd_code')){
					session('step','3');
					$this->ajax_r(1,'','验证成功！');
				}else{
					$this->ajax_r(0,'','验证失败！');
				}
			}else{
				session('step','1');
				$this->ajax_r(0,'','验证手机不存在,请重新验证！');
			}
		}
	}
	
	public function forget_pwd_rest(){
		$moblie=session('forget_user');
		if($moblie){
			$pwd=I('post.pwd');
			
			$data['password']=passport_encrypt($pwd,'wnsc');
			$users=M('users');
			
			$rs=$users->where("phone=$moblie")->save($data);
			if($rs){
				session('step','4');
				session('forget_user',null);
				session('forget_pwd_code',null);
				$this->ajax_r(1,$rs,'密码重置成功！');
			}else{
				$this->ajax_r(0,$rs,'密码重置失败！');
			}
		}else{
			$this->ajax_r(0,'','验证手机不存在,请重新验证！');
		}
	}
	//解密
	public function jiemi(){
		$password=I('get.pass');
		$pass=passport_decrypt($password,'wnsc');
		echo $pass;
	}
	
	public function center(){
		$order=M('order');
		$uid=session('user_id');
		if($uid){
			//红包
			$daiyan=get_userdaiyan_list($uid);
			$daiyan_price=0;
			foreach($daiyan as $key=>$value){
				$daiyan_price=$daiyan_price+$value['recommend_price'];
			}
			$this->assign('daiyan_price',$daiyan_price);
			//未付款
			$wcount=$order->where('buyer_id='.$uid.' and order_state="10" and refund_state=0 and tuanselect=0')->count();
			//待拼团
			$dptcount=$order->where('buyer_id='.$uid.' and order_state="20" and refund_state=0 and tuanselect=1 and tuan_is_success=0')->count();
			//已付款待发货
			$dfcount=$order->where('buyer_id='.$uid.' and order_state="20" and refund_state=0 and tuanselect=0')->count();
			//拼团成功
			$ptcount=$order->where('buyer_id='.$uid.' and order_state="20" and refund_state=0 and tuanselect=1 and tuan_is_success=1')->count();
			$dfcount=$dfcount+$ptcount;
			//已发货待收货
			$dscount=$order->where('buyer_id='.$uid.' and order_state="30" and refund_state=0 and tuanselect=0')->count();
			//待评价
			$dpcount=$order->where('buyer_id='.$uid.' and order_state="40" and  tuanselect=0 and refund_state="0" and evaluation_state="0"')->count();
			//退换货
			$thcount=$order->where('buyer_id='.$uid.' and  refund_state>0 and refund_state<5 ')->count();
			$this->assign('thcount',$thcount);
			$this->assign('wcount',$wcount);
			$this->assign('dptcount',$dptcount);
			$this->assign('dfcount',$dfcount);
			$this->assign('dscount',$dscount);
			$this->assign('dpcount',$dpcount);
		}
		
		$this->display('center2');
	}
	
	
	//收藏 type=1 为商品 2为店铺
	public function collect(){
		$user_id=session('user_id');
		if($_POST&&$user_id){
			$data=I('post.');
			if($data['type']==1&&$data['id']){
				$newdata['shop_id']=$data['id'];
				$newdata['user_id']=$user_id;
				$users_shopcollect=M('users_shopcollect');
				$rs=$users_shopcollect->where($newdata)->select();
				if($rs){
					$this->ajax_r(0,'','该商品已收藏！');
				}else{
					$newdata['addtime']=time();
					$rs=$users_shopcollect->add($newdata);
					$this->ajax_r(1,'','收藏成功！');
				}
			}
			if($data['type']==2){
				$newdata['shore_id']=$data['id'];
				$newdata['user_id']=$user_id;
				$users_storecollect=M('users_storecollect');
				$rs=$users_storecollect->where($newdata)->select();
				if($rs){
                    $users_storecollect->where($newdata)->delete();
					$this->ajax_r(2,'','取消收藏成功！');
				}else{
					$newdata['addtime']=time();
					$rs=$users_storecollect->add($newdata);
					$this->ajax_r(1,'','收藏成功！');
				}
			}
		}else{
			$this->ajax_r(0,'','请先登录！');
		}
	}
	//取消收藏 type=1 为商品 2为店铺
	public function delcollect(){
		$user_id=session('user_id');
		if($_POST&&$user_id){
			$data=I('post.');
			if($data['type']==1&&$data['id']){
				$newdata['shop_id']=$data['id'];
				$newdata['user_id']=$user_id;
				$users_shopcollect=M('users_shopcollect');
				$rs=$users_shopcollect->where($newdata)->delete();
				if($rs){
					$this->ajax_r(0,'','删除成功！');
				}else{
					$this->ajax_r(0,'','收藏失败！');
				}
			}
			if($data['type']==2){
				$newdata['shore_id']=$data['id'];
				$newdata['user_id']=$user_id;
				$users_storecollect=M('users_storecollect');
				$rs=$users_storecollect->where($newdata)->delete();
				if($rs){
					$this->ajax_r(0,'','删除成功！');
				}else{
					$this->ajax_r(0,'','收藏失败！');
				}
			}
		}
	}
	public function info(){
		$users=M('users');
		$user_id=session('user_id');
		if($_POST){
			$data=I('post.');
			$info['id']=$user_id;
			$info['username']=$data['nickname'];
			$info['sex']=$data['user_sex'];
			$rs=$users->save($info);
			if($rs){
				$this->ajax_r(1,'','修改成功！');
			}else{
				$this->ajax_r(0,'','没有修改！');
			}
		}else{
			$user_info=$users->find($user_id);
			$this->assign('user_info',$user_info);
			$this->display();
		}
		
	}
	function editprofile(){
		if($_POST){
			$users=M('users');
			$user_id=session('user_id');
			$data=I('post.');
			if($data['item']=='birthday'){
				$info['id']=$user_id;
				$info['birthday']=$data['value'];
			}
			if($data['item']=='headimg'){
				$info['id']=$user_id;
				$info['user_pic']=$data['value'];
			}
			$rs=$users->save($info);
			if($rs){
				$this->ajax_r(1,'','修改成功！');
			}else{
				$this->ajax_r(0,'','没有修改！');
			}
		}
	}
	public function address(){
		
		$this->display();
	}	
	
	public function get_addrsss(){
		$user_id=session('user_id');
		$users_address=M('users_address');
		$last=I('post.last');
    	$amount=I('post.amount');
		
		$address=$users_address->where('user_id='.$user_id)->limit($last,$amount)->select();
		if($address){
			foreach($address as $key=>$value){
				$province=getProvinceInfo($value['province']);
				$city=getCityInfo($value['city']);
				$district=getDistrictInfo($value['district']);
				if($value['is_default']==1){
					$is_default='addl-red';
				}else{
					$is_default='addl-hui';
				}
				if(I('get.type')==1){
					$res[]['info']='<div class="address-add-box"><dl class="queren" data="'.$value['address_id'].'"><dt><strong>'.$value['consigner'].'&nbsp;&nbsp;</strong><span>'.$value['mobile'].'</span></dt><dd>'.$province['province_name'].$city['city_name'].$district['district_name'].$value['address'].'</dd></dl><div class="address-bottom"><div class="add-left"><!--a标签选中状态class为addl-red，不选中状态为addl-hui--><span><a href="javascript:void(0)" data-address_id="'.$value['address_id'].'" class="add_selected '.$is_default.'"></a></span><em>设为默认地址</em></div><div class="add-right"><a href="javascript:edit('.$value['address_id'].')"><i class="iconfont"></i><span>编辑</span></a><a href="javascript:void(0)" data-address_id="'.$value['address_id'].'" class="address-delete"><i class="iconfont"></i><span>删除</span></a></div></div></div>';
				}elseif(I('get.type')==2){
					$res[]['info']='<div class="address-add-box"><dl class="queren2" data="'.$value['address_id'].'"><dt><strong>'.$value['consigner'].'&nbsp;&nbsp;</strong><span>'.$value['mobile'].'</span></dt><dd>'.$province['province_name'].$city['city_name'].$district['district_name'].$value['address'].'</dd></dl><div class="address-bottom"><div class="add-left"><!--a标签选中状态class为addl-red，不选中状态为addl-hui--><span><a href="javascript:void(0)" data-address_id="'.$value['address_id'].'" class="add_selected '.$is_default.'"></a></span><em>设为默认地址</em></div><div class="add-right"><a href="javascript:edit('.$value['address_id'].')"><i class="iconfont"></i><span>编辑</span></a><a href="javascript:void(0)" data-address_id="'.$value['address_id'].'" class="address-delete"><i class="iconfont"></i><span>删除</span></a></div></div></div>';
				
				}else{
					$res[]['info']='<div class="address-add-box"><dl><dt><strong>'.$value['consigner'].'&nbsp;&nbsp;</strong><span>'.$value['mobile'].'</span></dt><dd>'.$province['province_name'].$city['city_name'].$district['district_name'].$value['address'].'</dd></dl><div class="address-bottom"><div class="add-left"><!--a标签选中状态class为addl-red，不选中状态为addl-hui--><span><a href="javascript:void(0)" data-address_id="'.$value['address_id'].'" class="add_selected '.$is_default.'"></a></span><em>设为默认地址</em></div><div class="add-right"><a href="javascript:edit('.$value['address_id'].')"><i class="iconfont"></i><span>编辑</span></a><a href="javascript:void(0)" data-address_id="'.$value['address_id'].'" class="address-delete"><i class="iconfont"></i><span>删除</span></a></div></div></div>';
				}

			}
		}else{
			$res[]['info']='<div class="no-data-div"><div class="no-data-img"><img src="/themes/wnsc_moblie/Public/images/bg_empty_data.png"></div><dl><dt>没有收货地址</dt><dd>点击下方按钮新增！</dd></dl></div>';
		}
		
		 $this->ajaxReturn($res);
		
		
	}
	public function addressInsert(){
		$user_id=session('user_id');
		$users_address=M('users_address');
		$is_default=M('users_address')->where("user_id=$user_id and is_default=1")->select();
		if($_POST){
			
			$data=I('post.');
			$newdata['user_id']=$user_id;
			$newdata['consigner']=$data['consignee'];
			$newdata['province']=$data['add_province_id'];
			$newdata['city']=$data['add_city_id'];
			$newdata['district']=$data['add_district_id'];
			$newdata['address']=$data['address_detail'];
			$newdata['mobile']=$data['mobile'];
			if(!$is_default){
				$newdata['is_default']=1;
			}
			if($data['address_id']!=0){
				$newdata['address_id']=$data['address_id'];
				$rs=$users_address->save($newdata);
				if($rs){
					$this->ajax_r('1','','保存成功');
				}else{
					$this->ajax_r('0','','保存失败');
				}
			}else{
			
				$rs=$users_address->add($newdata);
				if($rs){
					$this->ajax_r('1','','添加成功');
				}else{
					$this->ajax_r('0','','添加失败');
				}
			}
		}else{
			$id=I('get.id');
			$rs=$users_address->find($id);
			
			$province=getProvinceInfo($rs['province']);
			$city=getCityInfo($rs['city']);
			$district=getDistrictInfo($rs['district']);
			$rs['province_name']=$province['province_name'];
			$rs['city_name']=$city['city_name'];
			$rs['district_name']=$district['district_name'];
			$this->ajax_r('0',$rs,'地址信息');
			$this->display();
		}
		
	}
	
	public function get_user_address(){
		$id=I('post.id');
		$users_address=M('users_address');
		$rs=$users_address->find($id);
		$this->ajax_r('1',$rs,'地址信息！');
	}
	public function addressisdefault(){
		$address_id=I('post.address_id');
		$user_id=session('user_id');
		$users_address=M('users_address');
		$users_address-> where('user_id='.$user_id)->setField('is_default',0);
		$users_address-> where('address_id='.$address_id)->setField('is_default',1);
		$this->ajax_r('1','','设置默认成功');
		
	}
			
	
	public function addressdelete(){
		$id=I('post.address_id');
		$users_address=M('users_address');
		$rs=$users_address->delete($id);
		if($rs){
			$this->ajax_r('1','','删除成功');
		}else{
			$this->ajax_r('0','','删除失败');
		}
	}
	 public function uploadify_wb()
    {
    	 if(I('get.isthumb')){
            $width  =   I('get.width','400');
            $height =   I('get.height','400');
        }else{
            $width  =   I('get.width');
            $height =   I('get.height');
        }
        if (!empty($_FILES)) {
            import("@.ORG.UploadFile");
            $upload = new \Think\Upload();
            $upload->maxSize = 2048000;
            $upload->allowExts = array('jpg','jpeg','gif','png','xls');
            $upload->savePath = "/";
            $upload->thumb = true; //设置缩略图
            $upload->imageClassPath = "@.ORG.Image";
            $upload->saveRule = uniqid; //上传规则
            $upload->thumbRemoveOrigin = true; //删除原图
            $info   =   $upload->upload();
            if(!$info) {
                echo 'error'.$upload->getError();// 上传错误提示错误信息
            }else{
                foreach($info as $file){
                    $path='/Uploads'.$file['savepath'].$file['savename'];
                    $name   =   $_SERVER["DOCUMENT_ROOT"].$path;
                    $imageArray =   getimagesize($name);
                    if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
                        $save_path=$_SERVER['DOCUMENT_ROOT'].$path;
                        $thumb_path=$_SERVER['DOCUMENT_ROOT'].'/Uploads'.$file['savepath'].'thumb_'.$file['savename'];
                        $image = new \Think\Image(); 
                        $image->open($save_path);
                        $image->thumb($width, $height)->save($save_path);
                    }
                    
                }
            }
           
            $data['saveURL']=$path;
            $data['state']='success';
            $data['status']=1;
            echo json_encode($data);

        }
    }
    
     public function uploadify_wb2()
    {
        if(I('get.isthumb')){
            $width  =   I('get.width','400');
            $height =   I('get.height','400');
        }else{
            $width  =   I('get.width');
            $height =   I('get.height');
        }
        if (!empty($_FILES)) {
            import("@.ORG.UploadFile");
            $upload = new \Think\Upload();
            $upload->maxSize = 2048000;
            $upload->allowExts = array('jpg','jpeg','gif','png','xls');
            $upload->savePath = "/";
            $upload->thumb = true; //设置缩略图
            $upload->imageClassPath = "@.ORG.Image";
            $upload->saveRule = uniqid; //上传规则
            $upload->thumbRemoveOrigin = true; //删除原图
            $info   =   $upload->upload();
            if(!$info) {
                echo 'error'.$upload->getError();// 上传错误提示错误信息
            }else{
                foreach($info as $file){
                    $path='/Uploads'.$file['savepath'].$file['savename'];
                    $name   =   $_SERVER["DOCUMENT_ROOT"].$path;
                    $imageArray =   getimagesize($name);
                    if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
                        $save_path=$_SERVER['DOCUMENT_ROOT'].$path;
                        $thumb_path=$_SERVER['DOCUMENT_ROOT'].'/Uploads'.$file['savepath'].'thumb_'.$file['savename'];
                        $image = new \Think\Image(); 
                        $image->open($save_path);
                        $image->thumb($width, $height)->save($save_path);
                    }
                    
                }
            }
           echo $path;
           

        }
    }
    
    function del_img(){
    	if($_POST){
    		$imgsrc=I('post.imgsrc');
    		$path = $_SERVER['DOCUMENT_ROOT'].$imgsrc;
			unlink($path);
    		
    	}
    }
     /**
     * 获取省列表
     */
    public function getProvince()
    {
    	$province_list = getProvinceList();
    	$this->ajaxReturn($province_list);
    }


    /**
     * 获取城市列表
     *
     * @return Ambigous <multitype:\think\static , \think\false, \think\Collection, \think\db\false, PDOStatement, string, \PDOStatement, \think\db\mixed, boolean, unknown, \think\mixed, multitype:, array>
     */
    public function getCity()
    {
        
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_list = getCityList($province_id);
        $this->ajaxReturn($city_list);
       
    }

    /**
     * 获取区域地址
     */
    public function getDistrict()
    {
       
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $district_list = getDistrictList($city_id);
        $this->ajaxReturn($district_list);
       
    }

    /**
     * 获取选择地址
     *
     * @return unknown
     */
    public function getSelectAddress()
    {

        $province_list = getProvinceList();
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $city_list = getCityList($province_id);
        $district_list =getDistrictList($city_id);
        $data["province_list"] = $province_list;
        $data["city_list"] = $city_list;
        $data["district_list"] = $district_list;
         $this->ajaxReturn($data);
    }	
    
    public function order(){
    	$uid=session('user_id');
    	$order=M('order');
    	//全部
    	$cccount=$order->where('buyer_id='.$uid.' and refund_state=0 ')->count();
    	//未付款
    	$wcount=$order->where('buyer_id='.$uid.' and refund_state=0  and order_state="10"')->count();
    	//待拼团
    	$dptount=$order->where('buyer_id='.$uid.' and refund_state=0  and tuanselect=1 and tuan_is_success=0 and order_state="20"')->count();
    	//拼团成功待发货  已付款待发货
    	$dfcount=$order->where('buyer_id='.$uid.' and refund_state=0  and order_state="20" and ((tuanselect=1 and tuan_is_success=1) or (tuanselect=0))')->count();
    	//已发货待收货
    	$dscount=$order->where('buyer_id='.$uid.' and refund_state=0   and order_state="30"')->count();
    	//待评价
    	$dpcount=$order->where('buyer_id='.$uid.'  and evaluation_state="0"  and refund_state=0  and order_state="40" ')->count();
    
    	
    	$this->assign('wcount',$wcount);
    	$this->assign('dfcount',$dfcount);
    	$this->assign('dptount',$dptount);
    	$this->assign('dscount',$dscount);
    	$this->assign('dpcount',$dpcount);
    	$this->assign('cccount',$cccount);
    	$this->display();
    }
    
    public function order_pt(){
    	$uid=session('user_id');
    	$order=M('order');
    	
    
    	//全部
    	$cccount=$order->where('buyer_id='.$uid.' and tuanselect=1 and order_state>="10"')->count();
    	//等待拼团
    	$wcount=$order->where('buyer_id='.$uid.' and order_state<="20" and order_state>="10" and refund_state=0 and tuanselect=1 and tuan_is_success=0')->count();
    	//拼团成功
    	$dfcount=$order->where('buyer_id='.$uid.' and order_state>="20" and refund_state=0 and tuanselect=1 and tuan_is_success=1')->count();
    	//拼团失败
    	$dscount=$order->where('buyer_id='.$uid.' and order_state="20" and refund_state>0 and tuanselect=1 and tuan_is_success=0' )->count();
    	
    	$this->assign('wcount',$wcount);
    	$this->assign('dfcount',$dfcount);
    	$this->assign('dscount',$dscount);
  		$this->assign('cccount',$cccount);
    	$this->display();
    }
    
    public function ajax_getorder_list(){
    	$order=M('order');
    	$uid=session('user_id');
    	$page=I('post.page');
    	$amount=6;
    	$where['buyer_id']=$uid;
    	$where['refund_state']='0';
    	if(I('post.stauts')==1){
    		//待付款
    		$where['order_state']='10';
          
    	}
    	if(I('post.stauts')==2){
    		//待拼单
    		$where['order_state']='20';
    		$where['tuanselect']=1;
    		$where['tuan_is_success']=0;
            $where['refund_state']='0';
    	}
    	if(I('post.stauts')==3){
    		//待发货
    		$where['_string'] = ' (order_state="20" and tuanselect=0)  OR ( order_state="20" and tuanselect=1 and tuan_is_success=1) ';
            $where['refund_state']='0';
    	}
    	if(I('post.stauts')==4){
    		//待收货
    		$where['order_state']='30';
            $where['refund_state']='0';
    	}
    	if(I('post.stauts')==5){
    		//待评价
    		$where['order_state']='40';
    		$where['evaluation_state']='0';
    	}
    	$last=($page-1)*$amount;
    	
		$order_list=$order->where($where)->limit($last,$amount)->order('order_id desc')->select();
		$count=$order->where($where)->count();
		$pages=ceil($count/$amount);
		$content;
		$res;
		foreach($order_list as $key=>$value){
			if($value['tuanselect']==0){
				if($value['refund_state']==0){
					if($value['order_state']=='0'){
						$top_info='已取消';
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment">已取消</a>
									</div>
								</div>';
					}
					if($value['order_state']=='10'){
						$top_info='等待付款';
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									<div class="order-bottom-btn">
										<a class="cancel-payment edit-order" data-id="'.$value['order_id'].'" data-type="close_order" >取消订单</a>
									</div>
		
									<div class="order-bottom-btn">
										<a href="'.U('pay/paychoose',array('order_cn'=>$value['order_sn'])).'" class="on-payment cur">立即付款</a>
									</div>
									<!-- 确认收货  -->
								</div>';
					}
					if($value['order_state']=='20'){
						$top_info='已付款，待发货';
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
									</div>
									<div class="order-bottom-btn">
										<a class="on-payment cur" >等待发货</a>
									</div>
								</div>';
					}
					if($value['order_state']=='30'){
						$top_info='已发货，待收货';
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
									</div>
									<div class="order-bottom-btn">
										<a class="on-payment cur edit-order"  data-id="'.$value['order_id'].'" data-type="queren_get">确认收货</a>
									</div>
								</div>';
					}
					if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
						$top_info='已收货，待评价';
						$endtime=$value['finnshed_time']+60*60*24*7;
						$nowtime=time();
						if($nowtime<$endtime){
							$tuihuo='<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
								</div>';
						}
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									'.$tuihuo.'<div class="order-bottom-btn">
										<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
									</div>
								</div>';
					}
				}else{
					if($value['refund_state']==1){
						$top_info='退款中';
						$button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">退款中</a>
									</div>
								</div>';
					}
					if($value['refund_state']==2){
						$top_info='同意退款';
						$button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">同意退款</a>
									</div>
								</div>';
					}
					if($value['refund_state']==3){
						$top_info='拒绝退款';
						$button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">拒绝退款</a>
									</div>
								</div>';
					}
					if($value['refund_state']==4){
						$top_info='等待财务待打款';
						$button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">待打款</a>
									</div>
								</div>';
					}
					if($value['refund_state']==5){
						$top_info='退款完成';
						$button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">退款完成</a>
									</div>
								</div>';
					}
                    if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
						$top_info='已收货，待评价';
						$endtime=$value['finnshed_time']+60*60*24*7;
						$nowtime=time();
						if($nowtime<$endtime){
							$tuihuo='<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
								</div>';
						}
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									'.$tuihuo.'<div class="order-bottom-btn">
										<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
									</div>
								</div>';
					}
				}
			}else{
				//已取消团购订单
					if($value['order_state']=='0'){
						$top_info='已取消';
						$button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment">已取消</a>
									</div>
								</div>';
					}
					if($value['refund_state']=='0'){
						if($value['order_state']=='10'){
							$top_info='等待付款';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="cancel-payment edit-order" data-id="'.$value['order_id'].'" data-type="close_order" >取消订单</a>
										</div>
			
										<div class="order-bottom-btn">
											<a href="'.U('pay/paychoose',array('order_cn'=>$value['order_sn'])).'" class="on-payment cur">立即付款</a>
										</div>
										<!-- 确认收货  -->
									</div>';
						}
						if($value['order_state']=='20'){
							if($value['tuanselect']==1){
								if($value['tuan_is_first_id']==0){
									$num=get_tuan($value['order_id']);
								}else{
									$num=get_tuan($value['tuan_is_first_id']);
								}
								if($num<$value['tuannum']){
									$tuan_type='拼团中';
								}else{
									$tuan_type='拼团成功';
								}
								
							}
							$top_info='已付款，待发货';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur" >'.$tuan_type.'</a>
										</div>
									</div>';
						}
						if($value['order_state']=='30'){
							$top_info='已发货，待收货';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur edit-order"  data-id="'.$value['order_id'].'" data-type="queren_get">确认收货</a>
										</div>
									</div>';
						}
						if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
							$top_info='已收货，待评价';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
										</div>
									</div>';
						}
					}else{
						if($value['refund_state']=='1'){
							$top_info='退款中';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">退款中</a>
										</div>
									</div>';
						}
						if($value['refund_state']=='2'){
							$top_info='同意退换货';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">同意退换货</a>
										</div>
									</div>';
						}
						if($value['refund_state']=='3'){
							$top_info='商家拒绝';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">商家拒绝</a>
										</div>
									</div>';
						}
						if($value['refund_state']=='4'){
							$top_info='退款/货完成';
							$button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">退款/货完成</a>
										</div>
									</div>';
						}
					}
			}
			if($value['shipping_fee']!=0){
				$value['shipping_fee']='运费：￥'.$value['shipping_fee'];
			}else{
				$value['shipping_fee']='免邮';
			}
			$shop_list=get_order_shop($value['order_id']);
			
			$order_list='<div class="order-list" id="order_list_'.$value['order_id'].'">
						<h2>
							<a href="'.U('store/index',array('id'=>$value['store_id'])).'">
								<i class="shop-icon"></i>
								<span>'.$value['store_name'].'</span>
							</a>
							<strong class="color">'.$top_info.'</strong>
						</h2>
						<ul class="order-list-goods">';
			foreach($shop_list as $k=>$v){
				$order_list.='<li>
					<a data="'.U('shop/index',array('id'=>$v['goods_id'])).'" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">
						<div class="goods-pic">
							<img src="'.$v['goods_image'].'" />
						</div>
						<dl class="goods-info">
							<dt class="goods-name"> '.$v['goods_name'].'</dt>
							<dd class="goods-attr">';
								if($v['goods_sku_spec']){
									$order_list.='<span>规格&nbsp;:&nbsp;'.$v['goods_sku_spec'].'</span>&nbsp;&nbsp;';
								}	
								if($v['goods_sku_style']){
									$order_list.='<span>颜色&nbsp;:&nbsp;'.$v['goods_sku_style'].'</span>';
								}			
							
				$order_list.='</dd>
							<dd class="goods-price price-color">
								<em>￥'.$v['goods_price'].'</em>
								<span class="goods-num">x'.$v['goods_num'].'</span>
							</dd>
						</dl>
					</a>
				</li>';
			}
			$youhui_price=get_coupon_price($value['buyer_id'],$value['coupon']);
			$order_amount=$value['order_amount']-$value['daiyan_sale']-$youhui_price;
			if($order_amount<0){
				$order_amount=0.00;
			}
			$count_num=M('order_goods')->where("order_id=".$value['order_id'])->sum('goods_num');	
			$order_list.='</ul>
							<div class="price">
								<span>共'.$count_num.'件商品&nbsp;&nbsp;实付：</span>
								<span class="price_sum price-color">¥<strong>'.$order_amount.'</strong></span>
								<span class="freight">('.$value['shipping_fee'].')</span>
							</div>
	
							<!-- 操作 -->
							<div class="order-bottom-con ">'.$button.'</div></div>
						';
				$res[]=$order_list;
		}
		$content='';
		foreach($res as $key=>$val){
			$content=$content.$val;
		}
		$rs['content']=$content;
		$rs['pages']=$pages;
    	$this->ajaxReturn($rs);
    }
    
    
    public function ajax_getorder_list_back(){
    	$order=M('order');
    	$uid=session('user_id');
    	$last=I('get.last');
    	$amount=I('get.amount');
    	$where['buyer_id']=$uid;
    	$where['refund_state']=array('GT',0);
    	$order_list=$order->where($where)->limit($last,$amount)->order('refund_state asc ,order_id desc')->select();
		
		$res;
		foreach($order_list as $key=>$value){
			if($value['refund_state']=='1'){
				$top_info='申请中';
				$button='<!-- 操作 -->
						<div class="order-bottom-con ">
							<div class="order-bottom-btn">
								<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">申请中</a>
							</div>
						</div>';
			}
			if($value['refund_state']=='2'){
				
				if($value['refund_type']=='1'){
					$top_info='商家同意，待打款';
					$button='<!-- 操作 -->
						<div class="order-bottom-con ">
							<div class="order-bottom-btn">
								<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">商家同意,待打款</a>
							</div>
						</div>';
				}else{
					if($value['refund_shipping_code']){
						$top_info='商家同意,待商家收货';
						$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<div class="order-bottom-btn">
									<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">商家同意,待商家收货</a>
								</div>
							</div>';
					}else{
						$top_info='商家同意,待买家发货';
						$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<div class="order-bottom-btn">
									<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">商家同意,待买家发货</a>
								</div>
							</div>';
					}
					
				}
				
			}
			if($value['refund_state']=='3'){
				$top_info='商家拒绝';
				$button='<!-- 操作 -->
						<div class="order-bottom-con ">
							<div class="order-bottom-btn">
								<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">商家拒绝</a>
							</div>
						</div>';
			}
			if($value['refund_state']=='4'){
				$top_info='待客服打款';
				$button='<!-- 操作 -->
						<div class="order-bottom-con ">
							<div class="order-bottom-btn">
								<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">待客服打款</a>
							</div>
						</div>';
			}
			if($value['refund_state']=='5'){
				$top_info='退款完成';
				$button='<!-- 操作 -->
						<div class="order-bottom-con ">
							<div class="order-bottom-btn">
								<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">退款完成</a>
							</div>
						</div>';
			}
		
			if($value['shipping_fee']!=0){
				$value['shipping_fee']='运费：￥'.$value['shipping_fee'];
			}else{
				$value['shipping_fee']='免邮';
			}
			$shop_list=get_order_shop($value['order_id']);
			
			$order_list='<div class="order-list" id="order_list_'.$value['order_id'].'">
						<h2>
							<a href="'.U('store/index',array('id'=>$value['store_id'])).'">
								<i class="shop-icon"></i>
								<span>'.$value['store_name'].'</span>
							</a>
							<strong class="color">'.$top_info.'</strong>
						</h2>
						<ul class="order-list-goods">';
			foreach($shop_list as $k=>$v){
				$order_list.='<li>
					<a data="'.U('shop/index',array('id'=>$v['goods_id'])).'" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">
						<div class="goods-pic">
							<img src="'.$v['goods_image'].'" />
						</div>
						<dl class="goods-info">
							<dt class="goods-name"> '.$v['goods_name'].'</dt>
							<dd class="goods-attr">';
								if($v['goods_sku_spec']){
									$order_list.='<span>规格&nbsp;:&nbsp;'.$v['goods_sku_spec'].'</span>&nbsp;&nbsp;';
								}	
								if($v['goods_sku_style']){
									$order_list.='<span>颜色&nbsp;:&nbsp;'.$v['goods_sku_style'].'</span>';
								}			
							
				$order_list.='</dd>
							<dd class="goods-price price-color">
								<em>￥'.$v['goods_price'].'</em>
								<span class="goods-num">x'.$v['goods_num'].'</span>
							</dd>
						</dl>
					</a>
				</li>';
			}
							
							

				$order_list.='</ul>

						<div class="price">
							<span>实付：</span>
							<span class="price_sum price-color">¥<strong>'.$value['order_amount'].'</strong></span>
							<span class="freight">('.$value['shipping_fee'].')</span>
						</div>

						<!-- 操作 -->
						<div class="order-bottom-con ">'.$button.'</div></div>';
				$res[]=$order_list;
		}
    	$this->ajaxReturn($res);
    }
     public function ajax_getorder_list_pt(){
    	$order=M('order');
    	$uid=session('user_id');
    	$last=I('get.last');
    	$amount=I('get.amount');
    	$status=I('get.stauts',0);
    	$where['buyer_id']=$uid;
    	$where['tuanselect']='1';
    	if($status==0){
    		$where['order_state']= array('EGT','10');
    	}
    	
    	if($status==1){
    		$where['tuan_is_success']= array('eq',0);
    		$where['order_state']= array('between',array('10','20'));
    	
    		$where['refund_state']='0';
    	}
    	if(I('get.stauts')==2){
    		$where['tuan_is_success']= array('eq',1);
    		$where['order_state']= array('EGT','20');
    		$where['refund_state']='0';
    	}
    	if(I('get.stauts')==3){
    		$where['tuan_is_success'] = array('eq',0);
    		$where['refund_state'] = array('gt',0);
    	}
		$order_list=$order->where($where)->limit($last,$amount)->order('order_id desc')->select();
		
		$res;
		foreach($order_list as $key=>$value){
			//已取消
			if($value['order_state']=='0'){
				$top_info='已取消';
				$button='<!-- 操作 -->
						<div class="order-bottom-con ">
							<div class="order-bottom-btn">
								<a class="cancel-payment">已取消</a>
							</div>
						</div>';
			}
			if($value['refund_state']=='0'){
				if($value['order_state']=='10'){
					$top_info='等待付款';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<!-- 取消订单 -->
								<div class="order-bottom-btn">
									<a class="cancel-payment edit-order" data-id="'.$value['order_id'].'" data-type="close_order" >取消订单</a>
								</div>
	
								<div class="order-bottom-btn">
									<a href="'.U('pay/paychoose',array('order_cn'=>$value['order_sn'])).'" class="on-payment cur">立即付款</a>
								</div>
								<!-- 确认收货  -->
							</div>';
				}
				if($value['order_state']=='20'){
					if($value['tuanselect']==1){
						if($value['tuan_is_first_id']==0){
							$num=get_tuan($value['order_id']);
						}else{
							$num=get_tuan($value['tuan_is_first_id']);
						}
						if($num<$value['tuannum']){
							$tuan_type='拼团中';
						}else{
							$tuan_type='拼团成功';
						}
						
					}
					$top_info='已付款，待发货';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<!-- 取消订单 -->
								<div class="order-bottom-btn">
									<a class="on-payment cur" >'.$tuan_type.'</a>
								</div>
							</div>';
				}
				if($value['order_state']=='30'){
					$top_info='已发货，待收货';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<!-- 取消订单 -->
								<div class="order-bottom-btn">
									<a class="on-payment cur edit-order"  data-id="'.$value['order_id'].'" data-type="queren_get">确认收货</a>
								</div>
							</div>';
				}
				if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
					$top_info='已收货，待评价';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<!-- 取消订单 -->
								<div class="order-bottom-btn">
									<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
								</div>
							</div>';
				}
			}else{
				if($value['refund_state']=='1'){
					$top_info='退款中';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<div class="order-bottom-btn">
									<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">退款中</a>
								</div>
							</div>';
				}
				if($value['refund_state']=='2'){
					$top_info='同意退换货';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<div class="order-bottom-btn">
									<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">同意退换货</a>
								</div>
							</div>';
				}
				if($value['refund_state']=='3'){
					$top_info='商家拒绝';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<div class="order-bottom-btn">
									<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">商家拒绝</a>
								</div>
							</div>';
				}
				if($value['refund_state']=='4'){
					$top_info='退款/货完成';
					$button='<!-- 操作 -->
							<div class="order-bottom-con ">
								<!-- 取消订单 -->
								<div class="order-bottom-btn">
									<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">退款/货完成</a>
								</div>
							</div>';
				}
			}
			
			
			if($value['shipping_fee']!=0){
				$value['shipping_fee']='运费：￥'.$value['shipping_fee'];
			}else{
				$value['shipping_fee']='免邮';
			}
			$shop_list=get_order_shop($value['order_id']);
			
			$order_list='<div class="order-list" id="order_list_'.$value['order_id'].'">
						<h2>
							<a href="'.U('store/index',array('id'=>$value['store_id'])).'">
								<i class="shop-icon"></i>
								<span>'.$value['store_name'].'</span>
							</a>
							<strong class="color">'.$top_info.'</strong>
						</h2>
						<ul class="order-list-goods">';
			foreach($shop_list as $k=>$v){
				$order_list.='<li>
					<a data="'.U('shop/index',array('id'=>$v['goods_id'])).'" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">
						<div class="goods-pic">
							<img src="'.$v['goods_image'].'" />
						</div>
						<dl class="goods-info">
							<dt class="goods-name"> '.$v['goods_name'].'</dt>
							<dd class="goods-attr">';
								if($v['goods_sku_spec']){
									$order_list.='<span>规格&nbsp;:&nbsp;'.$v['goods_sku_spec'].'</span>&nbsp;&nbsp;';
								}	
								if($v['goods_sku_style']){
									$order_list.='<span>颜色&nbsp;:&nbsp;'.$v['goods_sku_style'].'</span>';
								}			
							
				$order_list.='</dd>
							<dd class="goods-price price-color">
								<em>￥'.$v['goods_price'].'</em>
								<span class="goods-num">x'.$v['goods_num'].'</span>
							</dd>
						</dl>
					</a>
				</li>';
			}
							
							

				$order_list.='</ul>

						<div class="price">
							<span>共1件商品&nbsp;&nbsp;实付：</span>
							<span class="price_sum price-color">¥<strong>'.$value['order_amount'].'</strong></span>
							<span class="freight">('.$value['shipping_fee'].')</span>
						</div>

						<!-- 操作 -->
						<div class="order-bottom-con ">'.$button.'</div></div>';
				$res[]=$order_list;
		}
    	$this->ajaxReturn($res);
    }
    
    public function order_info(){
    	
    	$uid=session('user_id');
		$isweixin=is_weixin();
    	if($isweixin){
    		$appid=C('weixin.APP_KEY');
			$appsecret=C('weixin.APP_SECRET');
			$weObj = new \Org\Util\Jssdk($appid,$appsecret);
			$wechat=$weObj->GetSignPackage();
			$this->assign('signPackage',$wechat);
    	}
    	if(I('get.id')){
    		$order_id=I('get.id');
	    	$order=M('order');
	    	$order_goods=M('order_goods');
	    	$order_info=$order->where('buyer_id='.$uid.' and order_id='.$order_id)->find();
	    	$order_goods_info=$order_goods->where('order_id='.$order_id)->select();
	    	$youhui_price=get_coupon_price($order_info['buyer_id'],$order_info['coupon']);
			if($order_info['tuanselect']==1&&$order_info['refund_state']==0&&$order_info['order_state']=='20'){
				if($order_info['tuan_is_first_id']==0){
                   $tuan_pay_time=$order_info['payment_time'];
				}else{
					$first_tuan=$order->find($order_info['tuan_is_first_id']);
                    $tuan_pay_time=$first_tuan['payment_time'];
				}
                $this->assign('tuan_pay_time',$tuan_pay_time);
            }
	    	$this->assign('order_goods_info',$order_goods_info);
        
	    	$this->assign('order_info',$order_info);
	    	$this->assign('order_num',$order_info['order_sn']);
	    	$this->assign('youhui_price',$youhui_price);
	    	$this->display();
    	}
    	if(I('get.order')){
    		$order_num=I('get.order');
	    	$order=M('order');
	    	$order_goods=M('order_goods');
	    	$order_info_s=$order->where("buyer_id=$uid and order_cn='$order_num'")->select();
	   
	    	$order_info_d=$order->where('buyer_id='.$uid.' and order_sn='.$order_num)->find();
	    	if($order_info_s){
	    		$order_info=$order->where('buyer_id='.$uid.' and order_id='.$order_info_s[0]['order_id'])->find();
	    		$order_goods_info=array();
	    		$youhui_price=0;
	    		foreach($order_info_s as $key=>$value){
	    			$order_good=$order_goods->where('order_id='.$value['order_id'])->select();
	    			$order_goods_info=array_merge($order_goods_info,$order_good);
	    			$youhui_price_s=get_coupon_price($value['buyer_id'],$value['coupon']);
	    			$youhui_price=$youhui_price+$youhui_price_s;
	    		}
	    	}elseif($order_info_d){
	    		$order_info=$order_info_d;
	    		$order_goods_info=$order_goods->where('order_id='.$order_info_d['order_id'])->select();
	    		$youhui_price=get_coupon_price($order_info_d['buyer_id'],$order_info_d['coupon']);
		    }
			if($order_info['tuanselect']==1&&$order_info['refund_state']==0&&$order_info['order_state']=='20'){
				if($order_info['tuan_is_first_id']==0){
                   $tuan_pay_time=$order_info['payment_time'];
				}else{
					$first_tuan=$order->find($order_info['tuan_is_first_id']);
                    $tuan_pay_time=$first_tuan['payment_time'];
				}
                $this->assign('tuan_pay_time',$tuan_pay_time);
            }
			$this->assign('order_goods_info',$order_goods_info);
		    $this->assign('order_info',$order_info);
		    $this->assign('order_num',$order_num);
		    $this->assign('youhui_price',$youhui_price);
		    $this->display();
	    }
    }
    
    function collect_shop(){
    	if($_POST){
    		$users_shopcollect=M('users_shopcollect');
	    	$uid=session('user_id');
	    	$last=I('post.last');
	    	$amount=I('post.amount');
			$rs_shop=$users_shopcollect
			->field('yt_shop.title,yt_shop.id,yt_shop.price,yt_shop.oprice,yt_shop.image,yt_users_shopcollect.id as cid')
			->join('yt_shop ON yt_shop.id = yt_users_shopcollect.shop_id','left')
			->where('yt_users_shopcollect.user_id='.$uid)
			->order('yt_users_shopcollect.addtime desc')
			->limit($last,$amount)
			->select();
		
			foreach($rs_shop as $key=>$value){
				$href=U('shop/index',array('id'=>$value['id']));
				$res[]['shouchang']='<dl><dt><a href="'.$href.'"><img src="'.$value['image'].'" width="100" height="100"></a></dt><dd><a href="'.$href.'" class="title"><p>'.$value['title'].'</p><em>￥'.$value['oprice'].'</em></a><span><strong>￥'.$value['price'].'</strong><a href="javascript:deleteCollection('.$value['cid'].')" class="s_out" style=" color:#999;font-size:14px;">删除</a> </span></dd></dl>';
			}
			$this->ajaxReturn($res);
    	}else{
    		$this->display();
    	}
    }
    function del_collect_shop(){
    	$where['user_id']=session('user_id');
    	$where['id']=I('post.id');
    	$users_shopcollect=M('users_shopcollect');
    	$rs_shop=$users_shopcollect->where($where)->delete();
    	$res['message']='取消成功';
    	$res['url']=U('user/collect_shop');
		$this->ajaxReturn($res);
    }
    
    function collect_store(){
    	if($_POST){
    		$users_storecollect=M('users_storecollect');
    		$uid=session('user_id');
    		$last=I('post.last');
	    	$amount=I('post.amount');
			$rs_store=$users_storecollect
				->field('yt_store.title,yt_store.id,yt_users_storecollect.id as sid,yt_store_template.portrait')
				->join('yt_store ON yt_store.id = yt_users_storecollect.shore_id','left')
				->join('yt_store_template ON yt_store_template.store_id = yt_users_storecollect.shore_id','left')
				->where('yt_users_storecollect.user_id='.$uid)
				->limit($last,$amount)
				->order('yt_users_storecollect.addtime desc')
				->select();
			
			foreach($rs_store as $key=>$value){
				$href=U('store/index',array('id'=>$value['id']));
				$res[]['dianpu']='<dl><dt><span> <a href="'.$href.'">'.$value['title'].'</a></span><em><a href="javascript:deleteFollow('.$value['sid'].')">删除收藏</a></em></dt><dd><i><a href="'.$href.'"><img src="'.$value['portrait'].'" width="100" height="100"></a></i><em><a href="'.$href.'">进店逛逛</a></em></dd></dl>';
			}
			$this->ajaxReturn($res);
    	}ELSE{
    		$this->display();
    	}
    	
	
    	
    }
    function del_collect_store(){
    	$where['user_id']=session('user_id');
    	$where['id']=I('post.id');
    	$users_storecollect=M('users_storecollect');
    	$users_storecollect->where($where)->delete();
    	$res['message']='删除成功';
    	$res['url']=U('user/collect_store');
		echo json_encode($res);
    }
    
    //取消订单
    function close_order(){
    	$order_id=I('post.order_id');
    	$order=M('order');
		$order_info_d=$order->find($order_id);
    	$status=get_order_status($order_info_d['order_sn'],'10');
		if($status){
    		$rs=$order->where('order_sn='.$order_info_d['order_sn'])->setField('order_state','0');
	    	if($rs){
	    		$this->ajax_r('1','','订单取消成功!');
	    	}else{
	    		$this->ajax_r('0','','订单取消失败!');
	    	}
    	}else{
    		$this->ajax_r('0','','订单取状态不正确,请刷新！');
    	}
	  
    	
    }
    
    //确认收货订单
    function confirm_order(){
    	$order_id=I('post.order_id');
    	$order=M('order');
    	$order_info=$order->find($order_id);
    	$status=get_order_status($order_info['order_sn'],'30');
    	
    	if($status){
    		$data['order_state']='40';
    		$data['finnshed_time']=time();
    		$rs=$order->where('order_id='.$order_id)->save($data);
    		if($rs){
              	//收货代言通知
				$uid=$order_info['buyer_id'];
                $order_add_time=$order_info['add_time'];
		        $user_info=M('users')->find($uid);
			
				if($user_info['wx_h5openid']){
					$openid=$user_info['wx_h5openid'];
					$url='https://m.58daiyan.com/user/daiyan.html';
					$keyword1=date('Y年m月d日  H:i',$order_add_time);
					$keyword2=$order_info['order_sn'];
					send_info_daiyan($openid,$url,$keyword1,$keyword2);
				}
	    		$message['code']=1;
	    		$message['message']='确认收货成功';
	    		$this->ajaxReturn($message);
	    		
	    	}else{
	    		$message['code']=0;
	    		$message['message']='确认收货失败';
	    		$this->ajaxReturn($message);
	    	}
    	}else{
    		$message['code']=0;
	    	$message['message']='订单取状态不正确,请刷新！！';
	    	$this->ajaxReturn($message);
    	}
    	
    }
    //第一次评价
    function reviewcommodity(){
    	$order_goods=M('order_goods');
    	$id=I('get.id');
    	$shop_list=$order_goods->where("order_id=$id")->select();
    	$this->assign('shop_list',$shop_list);
    	$this->display();
    }
    
    function addgoodsevaluate(){
    	$goodsEvaluate=json_decode($_POST['goodsEvaluate'],true);
    	$order_id=I('post.order_id');
    	$flag=true;
    	$uid=session('user_id');
    	$userinfo=M('users')->find($uid);
    	$order_goods_evaluate=M('order_goods_evaluate');
    	
    	foreach($goodsEvaluate as $key=>$value){
    		$data=array();
    		$data['order_id']=$order_id;
    		$data['goods_id']=$value['order_goods_id'];
    		$data['uid']=$uid;
    		$data['content']=$value['content'];
    		$data['addtime']=time();
    		$data['image']=$value['imgs'];
    		$data['member_name']=$userinfo['username'];
    		if($value['is_anonymous']){
    			$data['is_anonymous']=1;
    		}else{
    			$data['is_anonymous']=0;
    		}
    		$data['scores']=$value['scores'];
    		
    		$rs=$order_goods_evaluate->add($data);
    		if(!$rs){
    			$flag=false;
    		}
    		aver_shop($value['order_goods_id']);
    	}
    	if($flag){
    		$order_data['order_id']=$order_id;
    		$order_data['evaluation_state']='1';
    		$order=M('order');
    		$rs=$order->save($order_data);
    		if(!$rs){
    			$flag=false;
    		}
    	}
    	if($flag){
    		$this->ajaxReturn(1);
    	}else{
    		$this->ajaxReturn(0);
    	}
    	
    }
    //追加评论
	function reviewagain(){
    	$order_goods=M('order_goods');
    	$id=I('get.id');
    	$shop_list=$order_goods->where("order_id=$id")->select();
    	$this->assign('shop_list',$shop_list);
    	$this->display();
    }
    
    function addgoodsevaluateagain(){
    	$goodsEvaluate=json_decode($_POST['goodsEvaluate'],true);
    	$order_id=I('post.order_id');
    	$flag=true;
    	
    	$order_goods_evaluate=M('order_goods_evaluate');
    	
    	foreach($goodsEvaluate as $key=>$value){
    		$data=array();
    		$map['order_id']=$order_id;
    		$map['goods_id']=$value['order_goods_id'];
    		
    		$data['again_content']=$value['content'];
    		$data['again_addtime']=time();
    		$data['again_image']=$value['imgs'];
    		
    		
    		$rs=$order_goods_evaluate->where($map)->save($data);
    		if(!$rs){
    			$flag=false;
    		}
    	
    		
    	}
    	if($flag){
    		$order_data['order_id']=$order_id;
    		$order_data['evaluation_state']='2';
    		$order=M('order');
    		$rs=$order->save($order_data);
    		if(!$rs){
    			$flag=false;
    		}
    	}
    	if($flag){
    		$this->ajaxReturn(1);
    	}else{
    		$this->ajaxReturn(0);
    	}
    }
    
    
    function evaluate(){
    	$order_goods_evaluate=M('order_goods_evaluate');
    	$uid=session('user_id');
    	$where_evaluate['uid']=$uid;
    	$where_evaluate['is_show']=1;
    	$evaluate_count=$order_goods_evaluate->where($where_evaluate)->count();
    
    	$this->assign('evaluate_count',$evaluate_count);
	    $Page       = new \Think\Page($evaluate_count,6);// 实例化分页类 传入总记录数
		$Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
	    $show = $Page->show();// 分页显示输出
	  	$evaluate_list=$order_goods_evaluate->where($where_evaluate)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
    	$this->assign('evaluate_list',$evaluate_list);
    	$this->assign('evaluate_page',$show);
    	$this->display();
    }
    function bonus(){
    	$this->display();
    }
    
    function ajax_getbonus(){
    	$coupon=M('coupon');
    	$page=I('post.page');
    	$amount=I('post.amount');
    	$uid=session('user_id');
    	
    	$count=$coupon
    			->field('yt_coupon_type.*,yt_coupon.coupon_code')
    			->join('yt_coupon_type on yt_coupon_type.coupon_type_id =yt_coupon.coupon_type_id')
    			->where("yt_coupon.uid=$uid")
    			->count();
		$pages=ceil($count/$amount);
		$last=($page-1)*$amount;
		
    	$coupon_list=$coupon
    			->field('yt_coupon_type.*,yt_coupon.coupon_code')
    			->join('yt_coupon_type on yt_coupon_type.coupon_type_id =yt_coupon.coupon_type_id')
    			->where("yt_coupon.uid=$uid")
				->order('yt_coupon.end_time desc')
    			->limit($last,$amount)
    			->select();
    	$list='';
    	foreach($coupon_list as $key=>$value){
            $time=time();
            $end_time=strtotime($value['end_time']);
            if($time> $end_time){
                $gouqi='background: #b9b4ae;';
                $url='javascript:;';
            }else{
                $gouqi='background: #FFA72A;';
                $url=U('list/coupons',array('type_id'=>$value['coupon_type_id']));

            }

    		$list.='<li>
				<div class="clearfloat"><span class="title" style="float:left;">优惠券</span><span style="float:right;">'.$value['coupon_code'].'</span></div>
				<a class="not_use" href="'.$url.'">
					<div class="p-r" style="'.$gouqi.'">
						<div class="active-msg"><img src="/themes/wnsc_moblie/newMobile/User/images/coupon_member.png" style="width:97%;">
							<div class="coupon-price" style="'.$gouqi.'">￥'.$value['money'].'</div>
							<div class="coupon-lose"><span>'.date('Y-m-d',strtotime($value['start_time'])).'</span>&nbsp;至&nbsp;<span>'.date('Y-m-d',strtotime($value['end_time'])).'</span></div>
						</div>
					</div>
				</a>
			</li>';
			
    	}
    	$res['content']=$list;
    	$res['pages']=$pages;
    	$this->ajaxReturn($res);
    	
    }
    
    function ajax_getdiyongbonus(){
    	$invoice=get_web_invoice();
    	
    	$daiyan=M('daiyan');
    	$page=I('post.page');
    	$amount=I('post.amount');
    	$uid=session('user_id');
    	
    	$count=$daiyan->where("uid=$uid")->count();
		$pages=ceil($count/$amount);
		$last=($page-1)*$amount;
		
    	$coupon_list=$daiyan
    			->field('yt_daiyan.*,yt_shop.image,yt_shop.title,yt_shop.dikouquan')
    			->join('yt_shop on yt_shop.id =yt_daiyan.shop_id')
    			->where("yt_daiyan.uid=$uid and yt_shop.dikouquan>0")
    			->limit($last,$amount)
    			->order('yt_daiyan.id desc')
    			->select();
    	$list='';
    	foreach($coupon_list as $key=>$value){
    		$start=date('Y.m.d',$value['time']);
    		$end=date('Y.m.d',$value['time']+60*60*24*$invoice['web_diyong']);
    		$guoqu_time=$value['time']+60*60*24*$invoice['web_diyong'];
    		$url="location='".U('shop/index',array('id'=>$value['shop_id']))."'";
            $time=time();
    		if($time>$guoqu_time){
    			$list.='<div class="content_list" onclick="'.$url.'">
					<div class="content_list_left"><img src="'.$value['image'].'" >
						<div class="c_title"><span  class="shopname">'.$value['title'].'</span><span style="shoptime">'.$start.' - '.$end.'</span></div>
					</div>
					<div class="content_list_right" style="background-image:linear-gradient(to right, rgb(117, 117, 117), rgb(128, 114, 114))">
					<div class="con_price"><span class="con_price_3" style="width: 100%;text-align: center;">￥'.$value['dikouquan'].'<em style="font-size: 12px;display: block;line-height:5px;"><br/>（已过期）</em></span> </div>
					</div>
					<!-- empty -->
					<div class="empty_top"></div>
					<div class="empty_dottom"></div>
				</div>';
    		}else{
    			$list.='<div class="content_list" onclick="'.$url.'">
					<div class="content_list_left"><img src="'.$value['image'].'" >
						<div class="c_title"><span  class="shopname">'.$value['title'].'</span><span style="shoptime">'.$start.' - '.$end.'</span></div>
					</div>
					<div class="content_list_right">
						<div class="con_price"><span class="con_price_3" style="width: 100%;text-align: center;">￥'.$value['dikouquan'].'</span></div>
					</div>
					<!-- empty -->
					<div class="empty_top"></div>
					<div class="empty_dottom"></div>
				</div>';
    		}
    		
			
    	}
    	$res['content']=$list;
    	$res['pages']=$pages;
    	$this->ajaxReturn($res);
    }
    
    
    
    function refunddetail(){
    	$order_id=I('get.order_id');
    	$order_shopid=I('get.order_shopid');
    	$order=M('order');
    	$order_goods=M('order_goods');
    	$order_info=$order->find($order_id);
    	$ordergood_info=$order->find($order_shopid);
    	
    	$this->assign('ordergood_info',$ordergood_info);
    	$this->assign('order_info',$order_info);
    	$this->display();
    }
    
    function refunddetail_save(){
    	
    	$order_id=I('post.order_id');
    	$order=M('order');
    	$order_info=$order->find($order_id);
    	$time=time()+60*60*24*7;
    	if($_POST&&$order_info['order_state']>10&&$order_info['finnshed_time']<$time){
    		$data['refund_state']='1';
    		$data['refund_amount']=I('post.refund_require_money');
    		$data['refund_reason']=I('post.refund_reason');
    		$data['refund_type']=I('post.refund_type');
    		$data['order_id']=I('post.order_id');
    		$data['refund_time']=time();
    		$rs=$order->save($data);
    		if($rs){
    			$this->ajax_r(1,'','提交成功');
    		}else{
    			$this->ajax_r(0,'','提交失败');
    		}
    	}else{
    		$this->ajax_r(0,'','订单信息错误');
    	}
    }
    
     function get_orderlogistics(){
	  	if($_POST){
	  		$order_id=I('post.order_id');
	  		$order=M('order');
	  		$order_info=$order->find($order_id);
	  		$result=getOrderTracesByJson($order_info['shipping_company_id'],$order_info['shipping_code'],'1304146','f0e280a8-d7b0-470a-b1bd-8d4a9584a78e','http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx');
         
	  		$this->ajaxReturn($result);
	  	}
	 }
	 
	 function refunddetail_update(){
	 	if($_POST){
	 		$data['refund_shipping_code']=I('post.wuliu_number');
	 		$data['refund_shipping_company_id']=I('post.wuliu_express');
	 		$data['refund_shipping_name']=I('post.wuliu_express_name');
	 		$data['order_id']=I('post.order_id');
	 		$order=M('order');
	 		$rs=$order->save($data);
	 		if($rs){
    			$this->ajax_r(1,'','提交成功');
    		}else{
    			$this->ajax_r(0,'','提交失败');
    		}
	 	}
	 }
	 
	 function back(){
	 	$order=M('order');
	 	$uid=session('user_id');
	 	$count=$order->where("buyer_id=$uid and refund_state>0")->count();
	 	$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
		$Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
	    $show = $Page->show();// 分页显示输出
	    $order_list=$order->where("buyer_id=$uid and refund_state>0")->select();
	 	$this->assign('order_list',$order_list);
	 	$this->assign('count',$count);
	 	$this->assign('page',$show);
	 	$this->display();
	 }
    function recharge(){
        $this->display();
    }
	 function withdrawal(){
	 	$uid=session('user_id');
	 	$userinfo=$this->userinfo;
	 	if($userinfo['ali_number']==null||$userinfo['ali_name']==null){
	 		$this->error('请先完善提现账号','/user/ali');
	 	}
	 	$this->assign('rs',$rs);
	 	$this->assign('page',$show);
	 	$this->assign('count',$count);
	 	$this->display();
	 }

    function rechargePay()
    {
        if ($_POST) {
            $user_id = session('user_id');
            $money = I('post.money');
            $order_cn = $this->build_order_no();
            $data['order_cn']=$order_cn;
            $order_sn = $this->build_order_no();
            //订单子编号
            $data['order_sn']=$order_sn;
            //卖家店铺id
            $data['store_id']=0;
            //卖家店铺名称
            $data['store_name']="58代言商城";
            //买家id
            $data['buyer_id']=$user_id;
            //订单生成时间
            $data['add_time']=time();
            //1WEB2mobile
            $data['order_from']=1;
            //商品总价格
            $data['goods_amount']=$money;
            //订单总价格
            $data['order_amount']=$money;
            //运费
            $data['shipping_fee']=0;
            $rs_order=M('order')->add($data);
            file_put_contents("pay.txt", date("Y-m-d H:i:s") . "rs_order:   " .print_r($rs_order, true). "\r\n", FILE_APPEND);
            if($rs_order){
                //$is_weixin = is_weixin();
                //if ($is_weixin) {
                    //使用jsapi接口
                    $jsApi = new \JsApi_pub();
                    //=========步骤1：网页授权获取用户openid============
				   file_put_contents("pay.txt", date("Y-m-d H:i:s") . "jsApi:   " .print_r($jsApi, true). "\r\n", FILE_APPEND);
                   file_put_contents("pay.txt", date("Y-m-d H:i:s") . "code:   " .print_r($_GET['code'], true). "\r\n", FILE_APPEND);

                //通过code获得openid
                    if (!isset($_GET['code'])) {
                        //触发微信返回code码
                        $baseUrl = urlencode('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
                        $url = $jsApi->createOauthUrlForCode($baseUrl);
                        Header("Location: $url");
                    } else {
                        // $baseUrl ='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];
                        //	echo $baseUrl;
                        //  die;
                        //获取code码，以获取openid
                        $code = $_GET['code'];
                        $jsApi->setCode($code);
                        $openid = $jsApi->getOpenId();
                    }
                file_put_contents("pay.txt", date("Y-m-d H:i:s") . "openid:   " .print_r($openid, true). "\r\n", FILE_APPEND);
                //=========步骤2：使用统一支付接口，获取prepay_id============
                    //使用统一支付接口
                    $unifiedOrder = new \UnifiedOrder_pub();
                    /*此处做数据库的查询  这里操作数据库把产品信息显示出来*/

                    //设置统一支付接口参数
                    //设置必填参数
                    //appid已填,商户无需重复填写
                    //mch_id已填,商户无需重复填写
                    //noncestr已填,商户无需重复填写
                    //spbill_create_ip已填,商户无需重复填写
                    //sign已填,商户无需重复填写
                    //

                    //设置统一支付接口参数
                    //设置必填参数
                    $unifiedOrder->setParameter("openid", $openid);//openid
                    $unifiedOrder->setParameter("body", "58代言商城余额充值订单");//商品描述
                    //自定义订单号，此处仅作举例
                    $unifiedOrder->setParameter("out_trade_no", $order_cn);//商户订单号
                    $unifiedOrder->setParameter("total_fee", $money);//总金额
                    $unifiedOrder->setParameter("notify_url", 'http://www.58daiyan.com/user/rechargeNotify');//通知地址
                    $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型
                    //非必填参数，商户可根据实际情况选填
                    //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
                    //$unifiedOrder->setParameter("device_info","XXXX");//设备号
                    //$unifiedOrder->setParameter("attach","XXXX");//附加数据
                    //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
                    //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
                    //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
                    //$unifiedOrder->setParameter("openid","XXXX");//用户标识
                    //$unifiedOrder->setParameter("product_id","XXXX");//商品ID


                    $prepay_id = $unifiedOrder->getPrepayId();

                     print_r($prepay_id);
                    //=========步骤3：使用jsapi调起支付============
                    $jsApi->setPrepayId($prepay_id);

                    $jsApiParameters = $jsApi->getParameters();
                    print_r($jsApiParameters);die();
                   /* $WEB_HOST = 'https://m.58daiyan.com';//填写的话 如 http://nicaicai.imwork.net 最后面不用加 /
                    $this->assign('HOSTS', $WEB_HOST);
                    $this->assign('jsApiParameters', $jsApiParameters);
                    $this->assign('order_cn', $order_cn);
                    $this->display();*/
               // }
			}

        }
    }
	 function get_tixian(){
	 	$uid=session('user_id');
	 	$users_log=M('users_log');
	 	$last=I('post.last');
    	$amount=I('post.amount');
	 	$rs=$users_log->where("uid=$uid")->limit($last,$amount)->order('id desc')->select();
	 	foreach($rs as $key=>$value){
	 		if($value['type']==1){
				$re="+";
			}else{
				$re="-";
			}
			$status=get_withdrawal($value['id']);
											
    		$list='<tr><td width="20%" align="center" style="line-height: 18px;"><span style="font-width: 800;">'.$value['other'].'</span><br/><p style="font-size: 12px;">'.$status.'</p></td><td width="10%" align="center">'.$re.$value['money'].'</td><td align="center">'.date('Y-m-d H:i:s',$value['addtime']).'</td></tr>';
			$res[]=$list;
    	}
    	$this->ajaxReturn($res);
	 }
	 
	  
	 function get_money(){
	 	$uid=session('user_id');
	 	$users_log=M('users_log');
	 	$users_getmoney=M('users_getmoney');
	 	$users=M('users');
	 	$users->startTrans();
	 	$users_getmoney->startTrans();
	 	$users_log->startTrans();
		$data=I('post.');
		$get_web_invoice=$this->$get_web_invoice;
		$userinfo=M('users')->find($uid);
		
	 	if($data['tx_money']>$userinfo['money']){
	 		$this->ajaxReturn(2);
	 	}
	 	if($data['tx_money']<$get_web_invoice['web_tixian_user']){
	 		$this->ajaxReturn(3);
	 	}
		
		
	 
	 	$log['uid']=$uid;
	 	$log['addtime']=time();
	 	$log['money']=$data['tx_money'];
	 	$log['type']=0;
	 	$log['other']='用户提现';
	 	$log['money_type']=3;
	 	$log_rs=$users_log->add($log);
	 	if($log_rs){
	 		$info['uid']=$uid;
		 	$info['money']=$data['tx_money'];
		 	$info['order_num']=$this->build_order_no();
		 	$info['time']=time();
		 	$info['status']=0;
		 	$info['type']=1;
		 	$info['log_id']=$log_rs;
		 	$rs_getmoney=$users_getmoney->add($info);
		 	if($rs_getmoney){
		 		$rs=$users->where('id='.$uid)->setDec('money',$data['tx_money']); 
		 		
		 		if($rs){
		 			$users->commit();
				 	$users_getmoney->commit();
				 	$users_log->commit();
		 			$this->ajaxReturn(1);
		 		}else{
		 			$users->rollback();
				 	$users_getmoney->rollback();
				 	$users_log->rollback();
		 			$this->ajaxReturn(0);
		 		}
		 	}else{
		 		$this->ajaxReturn($users_getmoney->_sql());
		 	}
	 	}else{
	 		$this->ajaxReturn($log);
	 	}
	}
	 
	public function build_order_no()
    {
        $no = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        //检测是否存在
        $db = M('users_getmoney');
        $info = $db->where(array('order_num'=>$no))->find();
        (!empty($info)) && $no = $this->build_order_no();
        return $no;
        
    }
     function ali(){
	 	$uid=session('user_id');
	 	if($_POST){
	 		$data=I('post.');
	 		$code=$data['code'];
	 		if($code==session('moblie_ali_code')){
	 			$info['id']=$uid;
	 			$info['ali_number']=$data['ali_number'];
	 			$info['ali_name']=$data['ali_name'];
	 			$rs=M('users')->save($info);
	 			if($rs){
	 				session('moblie_ali_code',null);
		 			$this->ajaxReturn(1);
		 		}else{
		 			$this->ajaxReturn(0);
		 		}
	 		}else{
	 			$this->ajaxReturn(2);
	 		}
	 	}else{
	 		$this->display();
	 	}
	 	
	 	
	 }
	 public function sms_captcha_ali(){
		$data=I('post.');
		$moblie=$data['mobile'];
		$code=rand(1000,9999);
		$rs=duanxin($code,$moblie);
	
		if($rs['resultcode']==0){
			session('moblie_ali_code',$code);
			$this->ajax_r(1,'','发送成功！');
		}else{
			$this->ajax_r(0,$rs['errordescription'],'发送失败！');
		}
	}
	
	public function daiyan(){
		$uid=session('user_id');
		//红包
		$daiyan=get_userdaiyan_list($uid);
		$daiyan_price=0;
		foreach($daiyan as $key=>$value){
			$daiyan_price=$daiyan_price+$value['recommend_price'];
		}
		$this->assign('daiyan_price',$daiyan_price);
  		$this->display();
    }
    public function crowdfunding(){
        $uid=session('user_id');
        $order=M('order');
        $count=$order
            ->field("yt_order.*,yt_order_goods.goods_id,yt_order_goods.goods_name,yt_order_goods.goods_price,yt_order_goods.goods_image")
            ->join('yt_order_goods ON yt_order.order_id = yt_order_goods.order_id','left')
            ->where("yt_order.buyer_id=$uid and yt_order_goods.goods_id in (SELECT goods_id from yt_crowdfunding) and (yt_order.order_state='20' or yt_order.order_state='30'or yt_order.order_state='40')")
            ->count();
        $wcount=$order
            ->field("yt_order.*,yt_order_goods.goods_id,yt_order_goods.goods_name,yt_order_goods.goods_price,yt_order_goods.goods_image")
            ->join('yt_order_goods ON yt_order.order_id = yt_order_goods.order_id','left')
            ->where("yt_order.buyer_id=$uid and yt_order_goods.goods_id in (SELECT goods_id from yt_crowdfunding) and yt_order.order_state='10'")
            ->count();
        $this->assign('count',$count);
        $this->assign('wcount',$wcount);
        $this->display();
    }
    public function get_daiyan(){
     	$uid=session('user_id');
	 	$order_goods=M('order_goods');
	 	$page=I('post.page');
		$amount=6;
		$count=$order_goods
    		->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->field("yt_order.buyer_name,yt_order.add_time,yt_order.payment_time,yt_order.shipping_time,yt_order.finnshed_time,yt_order.order_state,yt_order.refund_state,yt_order_goods.*")
    		->where("yt_order_goods.recommend_id=$uid and yt_order.order_state>'10' and yt_order_goods.recommend_type=0")
    		->count();
		$pages=ceil($count/$amount);
		$last=($page-1)*$amount;
	 	$rs=$order_goods
    		->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->field("yt_order.buyer_name,yt_order.add_time,yt_order.payment_time,yt_order.shipping_time,yt_order.finnshed_time,yt_order.order_state,yt_order.refund_state,yt_order_goods.*")
    		->where("yt_order_goods.recommend_id=$uid and yt_order.order_state>'10' and yt_order_goods.recommend_type=0")
    		->limit($last,$amount)
    		->order('yt_order_goods.rec_id desc')
    		->select();
	 	//print_r($order_goods->getLastSql());
    	$html='';
	 	foreach($rs as $key=>$value){
	 		
	 		$url=U('shop/index',array('id'=>$value['goods_id']));
	 		if($value['refund_state']==0){
	 			if($value['order_state']==0){
	 				$type="已取消";
	 				$time='下单时间：'.date('Y-m-d',$value['add_time']);
	 			} 
	 			if($value['order_state']==10){
	 				$type="未付款";
	 				$time='下单时间：'.date('Y-m-d',$value['add_time']);
	 			} 
	 			if($value['order_state']==20){
	 				$type="已付款";
	 				$time='付款时间：'.date('Y-m-d',$value['payment_time']);
	 			} 
	 			if($value['order_state']==30){
	 				$type="已发货";
	 				$time='发货时间：'.date('Y-m-d',$value['shipping_time']);
	 			} 
	 			if($value['order_state']==40){
	 				if($value['lock_state']==0){
	 					$type="已收货";
	 					$time='收货时间：'.date('Y-m-d',$value['finnshed_time']);
	 				}
	 				if($value['lock_state']==1){
	 					$type="已入账";
	 					$time='收货时间：'.date('Y-m-d',$value['finnshed_time']);
	 				}
	 			} 
			}else{
				if($value['refund_state']==1) $type="申请退款";
				if($value['refund_state']==2) $type="同意退款";
				if($value['refund_state']==3) $type="拒绝退款";
				if($value['refund_state']==4) $type="退款完成";
				
			}
	 		$html.='<div class="single_item shouchang" id="more_element_2">
				<dl>
					<dt>
						<a href="'.$url.'">
							<img src="'.$value['goods_image'].'" width="100" height="100">
						</a>
					</dt>
					<dd>
						<a href="'.$url.'" class="title">
							<p>'.$value['goods_name'].'</p>
							
						</a>
						<span style="line-height: 7px;height:75px">
							<strong style="font-size: 12px;line-height: 20px;height:20px">购买人：'.$value['buyer_name'].'</strong>
							<strong style="font-size: 12px;line-height: 20px;height:20px;color:#000;">红包：￥'.$value['recommend_price'].'</strong>
							<strong style="font-size: 12px;line-height: 20px;height:20px">状态：'.$type.'</strong>
							<a class="zq_button" style="color: #fff;margin-top: -39px;">'.$time.'</a> 
						</span>
					</dd>
				</dl>
			</div>';
			
    	}
    	$res['content']=$html;
		$res['pages']=$pages;
    	$this->ajaxReturn($res);
    
    }
    public function get_daiyan2(){
     	$uid=session('user_id');
	 	$order_goods=M('order_goods');
	 	$page=I('post.page');
		$amount=6;
		$count=$order_goods
    		->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->field("yt_order.buyer_name,yt_order.add_time,yt_order.payment_time,yt_order.shipping_time,yt_order.finnshed_time,yt_order.order_state,yt_order.refund_state,yt_order_goods.*")
    		->where("yt_order_goods.recommend_id=$uid and yt_order.order_state>'10' and yt_order_goods.recommend_type=1")
    		->count();
		$pages=ceil($count/$amount);
		$last=($page-1)*$amount;
	 	$rs=$order_goods
    		->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->field("yt_order.buyer_name,yt_order.add_time,yt_order.payment_time,yt_order.shipping_time,yt_order.finnshed_time,yt_order.order_state,yt_order.refund_state,yt_order_goods.*")
    		->where("yt_order_goods.recommend_id=$uid and yt_order.order_state>'10' and yt_order_goods.recommend_type=1")
    		->limit($last,$amount)
    		->order('yt_order_goods.rec_id desc')
    		->select();
    	$html='';
	 	foreach($rs as $key=>$value){
	 		$lp_shop_info=M('shop')->find($value['recommend_shop_id']);
	 		$url=U('shop/index',array('id'=>$value['goods_id']));
	 		if($value['refund_state']==0){
	 			if($value['order_state']==0){
	 				$type="已取消";
	 				$time='下单时间：'.date('Y-m-d',$value['add_time']);
	 			} 
	 			if($value['order_state']==10){
	 				$type="未付款";
	 				$time='下单时间：'.date('Y-m-d',$value['add_time']);
	 			} 
	 			if($value['order_state']==20){
	 				$type="已付款";
	 				$time='付款时间：'.date('Y-m-d',$value['payment_time']);
	 			} 
	 			if($value['order_state']==30){
	 				$type="已发货";
	 				$time='发货时间：'.date('Y-m-d',$value['shipping_time']);
	 			} 
	 			if($value['order_state']==40){
	 				if($value['lock_state']==0){
	 					$type="已收货";
	 					$time='收货时间：'.date('Y-m-d',$value['finnshed_time']);
	 				}
	 				if($value['lock_state']==1){
	 					$type="已入账";
	 					$time='收货时间：'.date('Y-m-d',$value['finnshed_time']);
	 				}
	 			} 
			}else{
				if($value['refund_state']==1) $type="申请退款";
				if($value['refund_state']==2) $type="同意退款";
				if($value['refund_state']==3) $type="拒绝退款";
				if($value['refund_state']==4) $type="退款完成";
				
			}
			
			if($value['recommend_orderid']){
				$lin_url=U('user/order_info',array('id'=>$value['recommend_orderid']));
			  	$linqu='<a href="'.$lin_url.'" class="title" style="width:25%;background: #eeeeee;height: 20vw;line-height: 20vw;text-align: center;margin-top: 6vw;border-radius: 5px;">已领取</a>';
				$order2=M('order')->find($value['recommend_orderid']);
				if($order2['order_state']==0){
	 				$type2="未领取";
	 			} 
	 			if($order2['order_state']==10){
	 				$type2="未领取";
	 			} 
	 			if($order2['order_state']==20){
	 				$type2="已领取,待发货";
	 			} 
	 			if($order2['order_state']==30){
	 				$type2="已发货";
	 			} 
	 			if($order2['order_state']==40){
	 				if($order2['lock_state']==0){
	 					$type2="已收货";
	 				}
	 				if($order2['lock_state']==1){
	 					$type2="已入账";
	 				}
	 			} 
			}else{
				if($value['order_state']>10){
					$linqu_url=U('pay/lipin',array('rec_id'=>$value['rec_id'],'recommend_shop_id'=>$value['recommend_shop_id'],'recommend_shop_num'=>$value['recommend_shop_num']));
					$linqu='<a href="'.$linqu_url.'" class="title" style="width:25%;background: #e7ba91;height: 20vw;line-height: 20vw;text-align: center;margin-top: 6vw;border-radius: 5px;color: #fff;">领取</a>';
					$type2='未领取';
				}else{
					$tishi="'该推荐用户还未付款，不能领取。'";
					$linqu='<a href="javascript:;" onclick="javaScript:alert('.$tishi.')" class="title" style="width:25%;background: #e7ba91;height: 20vw;line-height: 20vw;text-align: center;margin-top: 6vw;border-radius: 5px;color: #fff;">领取</a>';
					$type2='未领取';
				}
				
			}
	 		$html.='<div class="single_item shouchang" id="more_element_2">
				<dl>
					<dt>
						<a href="'.$url.'">
							<img src="'.$value['goods_image'].'" width="100" height="100">
						</a>
					</dt>
					<dd>
						<a href="'.$url.'" class="title" style="width:100%">
							<p>'.$value['goods_name'].'</p>
							
						</a>
						<span style="line-height: 7px;height:110px;width:70%">
							<strong style="font-size: 12px;line-height: 20px;height:20px;width: 100%;">购买人：'.$value['buyer_name'].'</strong>
							<strong style="font-size: 12px;line-height: 20px;height:20px;width: 100%;">购买订单状态：'.$type.'</strong>
							<strong style="font-size: 12px;line-height: 20px;height:20px;color:#000;width: 100%;"><em style="display: block; float: left; max-width: 75%; height: 20px;overflow: hidden;">礼品：'.$lp_shop_info['title'].'</em>*'.$value['recommend_shop_num'].'</strong>
							<strong style="font-size: 12px;line-height: 20px;height:20px;width: 100%;">礼品订单状态：'.$type2.'</strong>
							<a class="zq_button" style="color: #fff;margin-top: 3px;float:left;">'.$time.'</a> 
						</span>'.$linqu.'</dd>
				</dl>
			</div>';
			
    	}
    	$res['content']=$html;
		$res['pages']=$pages;
    	$this->ajaxReturn($res);
    
    }
	public function bduser(){
		$uid=session('user_id');
		$user_info=M('users')->find($uid);
		$this->assign('user_info',$user_info);
		$this->display();
	}
	public function bd_phone(){
		$uid=session('user_id');
		$user_info=M('users')->find($uid);
		$this->assign('user_info',$user_info);
		$this->display();
	}
	public function phone_bd(){
		$phone=I('post.mobile');
		$password=I('post.password');
		$sms_captcha=I('post.sms_captcha');
		$is_phone=M('users')->where("phone='".$phone."' and ishidden=1")->find();
		$uid=session('user_id');
		$y_userinfo=M('users')->find($uid);
	
		if($sms_captcha==session('bd_moblie_code')){
			if($is_phone){
				$uid=session('user_id');
				$user_info=M('users')->find($uid);
				$newdata['id']=$is_phone['id'];
				$newdata['password']=passport_encrypt($password,'wnsc');
				if($y_userinfo['wx_unionid']){
					$newdata['wx_openid']=$y_userinfo['wx_openid'];
					$newdata['wx_name']=$y_userinfo['wx_name'];
					$newdata['wx_unionid']=$y_userinfo['wx_unionid'];
				}
				if($y_userinfo['wb_openid']){
					$newdata['wb_openid']=$y_userinfo['wb_openid'];
					$newdata['wb_name']=$y_userinfo['wb_name'];
				}
				if($y_userinfo['qq_openid']){
					$newdata['qq_openid']=$y_userinfo['qq_openid'];
					$newdata['qq_name']=$y_userinfo['qq_name'];
				}
				
				$new_rs=M('users')->save($newdata);
				if($new_rs){
					M('users')->delete($uid);
					session('user_id',$is_phone['id']);
				}
			}else{
				$newdata['id']=$uid;
				$newdata['password']=passport_encrypt($password,'wnsc');
				$newdata['phone']=$phone;
				$new_rs=M('users')->save($newdata);
			}
			if($new_rs){
				$msg['code']=1;
				$msg['msg']='手机绑定成功';
			}else{
				$msg['code']=0;
				$msg['msg']='手机绑定失败';
			}
		}else{
			$msg['code']=0;
			$msg['msg']='验证码错误';
		}
		
		$this->ajaxReturn($msg);
	}
	
	public  function  change_phone(){
        $uid=session('user_id');
        $user_info=M('users')->find($uid);
        $this->assign('user_info',$user_info);
        if(session('bd_moblie_yz')==1){
            $step=2;
		}else{
            $step=1;
		}
        $this->assign('step',$step);
    	$this->display();
	}
	public function  change_phone_yz(){
    	if($_POST){
			$data=I('post.');
			$code1=$data['sms_captcha'];
            $code2=session('bd_moblie_code');
            if($code1==$code2){
                session('bd_moblie_code',null);
                session('bd_moblie_yz',1);
                $msg['code']=1;
                $msg['msg']='验证成功，正在进入下一步...';
            }else{
                $msg['code']=0;
                $msg['msg']='验证码错误';
			}
			$this->ajaxReturn($msg);
		}
	}

    public function  change_phone_yzbd(){
        if($_POST){
            $data=I('post.');
            $code1=$data['sms_captcha'];
            $mobile=$data['mobile'];
            $code2=session('bd_moblie_code');
            if($code1==$code2){
                session('bd_moblie_code',null);
                session('bd_moblie_yz',0);
                $data['id']=session('user_id');
                $data['phone']=$mobile;
                $rs=M('users')->save($data);
                if($rs){
                    $msg['code']=1;
                    $msg['msg']='手机号改绑成功！';
				}else{
                    $msg['code']=0;
                    $msg['msg']='手机号改绑！';
				}
            }else{
                $msg['code']=0;
                $msg['msg']='验证码错误';
            }
            $this->ajaxReturn($msg);
        }
    }


	public function weixin_bd(){
		$openid=session('openid');
		$unionid=session('unionid');
		$is_bd=M('users')->where("wx_unionid='".$unionid."'")->find();
		$uid=session('user_id');
		if($is_bd){
			$user_info=M('users')->find($uid);
			if($is_bd['phone']&&$user_info['phone']){
				$msg['code']=0;
				$msg['msg']='该微信账户已被绑定，不可合并';
			}else{
				$msg['code']=2;
				$msg['msg']='该微信账户已被绑定,是否合并';
			}
			
		}else{
			$data['id']=$uid;
			$data['wx_openid']=$openid;
			$data['wx_unionid']=$unionid;
			$rs=M('users')->save($data);
			if($rs){
				$msg['code']=1;
				$msg['msg']='微信绑定成功';
			}else{
				$msg['code']=0;
				$msg['msg']='微信绑定失败';
			}
		}
		$this->ajaxReturn($msg);
	}
	
	public function wx_qjbd(){
		$unionid=I('post.unionid');
		$uid=I('post.uid');
		$is_bd=M('users')->where("wx_unionid='".$unionid."'")->find();
		if($is_bd){
			$user_info=M('users')->find($uid);
			if($is_bd['phone']&&$user_info['phone']){
				$msg['code']=0;
				$msg['msg']='该微信账户已被绑定，不可合并';
			}else{
				//微信合并
				$data['wx_name']=$is_bd['wx_name'];
				$data['wx_openid']=$is_bd['wx_openid'];
				$data['wx_unionid']=$is_bd['wx_unionid'];
				if($is_bd['phone']){
					if($is_bd['wb_openid']==null&&$user_info['wb_openid']){
						$data['wb_openid']=$user_info['wb_openid'];
						$data['wb_name']=$user_info['wb_name'];
					}
					if($is_bd['qq_openid']==null&&$user_info['qq_openid']){
						$data['qq_openid']=$user_info['qq_openid'];
						$data['qq_name']=$user_info['qq_name'];
					}
					$data['id']=$is_bd['id'];
					$deleid=$user_info['id'];
				}else{
					if($is_bd['wb_openid']&&$user_info['wb_openid']==null){
						$data['wb_openid']=$is_bd['wb_openid'];
						$data['wb_name']=$is_bd['wb_name'];
					}
					if($is_bd['qq_openid']&&$user_info['qq_openid']==null){
						$data['qq_openid']=$is_bd['qq_openid'];
						$data['qq_name']=$is_bd['qq_name'];
					}
					$data['id']=$user_info['id'];
					$deleid=$is_bd['id'];
				}
				$rs=M('users')->save($data);
				if($rs){
					M('users')->delete($deleid);
					$msg['code']=1;
					$msg['msg']='微信绑定成功';
				}else{
					$msg['code']=0;
					$msg['msg']='微信绑定失败';
				}
			}
			$this->ajaxReturn($msg);
		}
		
	}
	
	
	public function weixin_twobd(){
		$openid=session('openid');
		$unionid=session('unionid');
		$is_bd=M('users')->where("wx_unionid='".$unionid."'")->find();
		$uid=session('user_id');
		if($is_bd){
			$user_info=M('users')->find($uid);
			if($is_bd['phone']&&$user_info['phone']){
				$msg['code']=0;
				$msg['msg']='该微信账户已被绑定，不可合并';
			}else{
				//微信合并
				$data['wx_name']=$is_bd['wx_name'];
				$data['wx_openid']=$openid;
				$data['wx_unionid']=$unionid;
				if($is_bd['phone']){
					if($is_bd['wb_openid']==null&&$user_info['wb_openid']){
						$data['wb_openid']=$user_info['wb_openid'];
						$data['wb_name']=$user_info['wb_name'];
					}
					if($is_bd['qq_openid']==null&&$user_info['qq_openid']){
						$data['qq_openid']=$user_info['qq_openid'];
						$data['qq_name']=$user_info['qq_name'];
					}
					$data['id']=$is_bd['id'];
					$deleid=$user_info['id'];
				}else{
					if($is_bd['wb_openid']&&$user_info['wb_openid']==null){
						$data['wb_openid']=$is_bd['wb_openid'];
						$data['wb_name']=$is_bd['wb_name'];
					}
					if($is_bd['qq_openid']&&$user_info['qq_openid']==null){
						$data['qq_openid']=$is_bd['qq_openid'];
						$data['qq_name']=$is_bd['qq_name'];
					}
					$data['id']=$user_info['id'];
					$deleid=$is_bd['id'];
				}
				$rs=M('users')->save($data);
				if($rs){
					M('users')->delete($deleid);
					$msg['code']=1;
					$msg['msg']='微信绑定成功';
				}else{
					$msg['code']=0;
					$msg['msg']='微信绑定失败';
				}
			}
			
		}else{
			$data['id']=$uid;
			$data['wx_openid']=$openid;
			$data['wx_unionid']=$unionid;
			$rs=M('users')->save($data);
			if($rs){
				$msg['code']=1;
				$msg['msg']='微信绑定成功';
			}else{
				$msg['code']=0;
				$msg['msg']='微信绑定失败';
			}
		}
		$this->ajaxReturn($msg);
	}
	
	

	
	//pc端微信回调
	public function weixin_return(){
		
		if(!$_GET['state']){
			exit("5001");
		}
		$AppID=C('wx_login.WEIXIN_APP_KEY');
		$AppSecret=C('wx_login.WEIXIN_APP_SECRET');
      

		$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_URL, $url);
		$json =  curl_exec($ch);
		curl_close($ch);
		$arr=json_decode($json,1);
		//得到 access_token 与 openid
        //  file_put_contents("111.txt", var_export($arr,TRUE));  
		//print_r($arr);    
		$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_URL, $url);
		$json =  curl_exec($ch);
		curl_close($ch);
		$arr=json_decode($json,1);
		if($_GET['uid']){
			$userinfo=M('users')->where("wx_unionid='".$arr['unionid']."'")->find();
			if($userinfo){
				$uid=I('get.uid');
				$user_info=M('users')->find($uid);
				if($userinfo['phone']&&$user_info['phone']){
					$url='http://www.58daiyan.com/user/oauth_login/token/0000';
					echo "<script language='javascript' type='text/javascript'>";
					echo "window.location.href='".$url."'";
					echo "</script>";
				}else{
					$this->assign('unionid',$arr["unionid"]);
					$this->assign('uid',$uid);
					$this->display();
				}
			}else{
				$user['wx_openid']=$arr["openid"];
				$user['wx_unionid']=$arr["unionid"];
				$user['wx_name']=$arr['nickname'];
				$user['id']=I('get.uid');
				M('users')->save($user);
			$url='http://www.58daiyan.com/user/bduser.html';
			echo "<script language='javascript' type='text/javascript'>";
			echo "window.location.href='".$url."'";
			echo "</script>";
			}
		}else{
          
			$userinfo=M('users')->where("wx_unionid='".$arr['unionid']."'")->find();
			$out_tock=generate_password();
			if($userinfo){
					$login_info['out_login']=$out_tock;
					$login_info['out_time']=time();
					//更新登录时间
					$date_time=date('Y-m-d H:i:s', time());
					if($userinfo['wx_openid']==null){
						$login_info['wx_openid']=$arr['openid'];
					}
					$login_info['id']=$userinfo['id'];
					$login_info['login_time']=$date_time;
					M('users')->save($login_info);
					
					//添加cookie 购物车
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						addcart($userinfo['id'],$v['shop_id'],$v['sku_id'],$v['num']);
					}
					cookie('cart_array',null);
				}else{
					//不存在创建游客账号,不是主动注销
					$youke['username']=$arr['nickname'];
					$youke['wx_name']=$arr['nickname'];
					$youke['password']="8888888";
					$youke['ishidden']=1;
					$youke['addtime']=time();
					$youke['user_pic']=$arr['headimgurl'];
					$youke['login_time']=date('Y-m-d H:i:s',time());
					$youke['sex']=$arr['sex'];
					$youke['wx_openid']=$arr['openid'];
					$youke['wx_unionid']=$arr['unionid'];
					$youke['wx_is_sub']=0;
					$youke['type']=1;
					$youke['out_login']=$out_tock;
					$youke['out_time']=time();
					$youke_id=M('users')->add($youke);
					
				}
			$url='http://www.58daiyan.com/user/oauth_login/token/'.$out_tock;
			echo "<script language='javascript' type='text/javascript'>";
			echo "window.location.href='".$url."'";
			echo "</script>";
		}
		
	
	
	}
	
	public function weibo(){
		session_start();
		
		vendor('Sinaweibo.Saetv2');
		//注意\

        $o = new \SaeTOAuthV2(C('wb_login.WEIBO_APP_KEY'),C('wb_login.WEIBO_APP_SECRET') );

                //url中不能缺少code
		if(isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = C('wb_login.WEIBO_MOBLIE_URL');

			try {
				//生成access token
				$token =$o->getAccessToken('code',$keys ) ;
			} catch (OAuthException $e) {
				//echo 1;die;
			}
		}
		
		if($token){
			$c = new \SaeTClientV2(C('wb_login.WEIBO_APP_KEY'),C('wb_login.WEIBO_APP_SECRET'),$token['access_token'] );
			
			$ms  = $c->home_timeline(); // done
			
			$uid_get = $c->get_uid();
			$uid = $uid_get['uid'];
			$user_message = $c->show_user_by_id($uid);//根据ID获取用户等基本信息
				file_put_contents("weibi.txt", var_export($user_message,true));  
			$wb_openid=$user_message['idstr'];
		
			if(session('user_id')==null){
				
				$userinfo=M('users')->where("wb_openid='".$wb_openid."'")->find();
				if($userinfo){
					//已绑定
					session('user_id',$userinfo['id']);
					session('user_name',$userinfo['username']);
					session('user_pic',$userinfo['user_pic']);
					session('login_time',$userinfo['login_time']);
					//更新登录时间
					$date_time=date('Y-m-d H:i:s', time());
					$login_info['id']=$userinfo['id'];
					$login_info['login_time']=$date_time;
					M('users')->save($login_info);
					
					//添加cookie 购物车
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						addcart($userinfo['id'],$v['shop_id'],$v['sku_id'],$v['num']);
					}
					cookie('cart_array',null);
					$this->success('登陆成功','/user/center');
				}else{
					//不存在创建游客账号,不是主动注销
					$youke['username']=$user_message['screen_name'];
					$youke['wb_name']=$user_message['screen_name'];
					$youke['password']="8888888";
					$youke['ishidden']=1;
					$youke['addtime']=time();
					$youke['user_pic']=$user_message['avatar_hd'];
					$youke['login_time']=date('Y-m-d H:i:s',time());
					$youke['sex']=0;
					$youke['wb_openid']=$user_message['idstr'];
					$youke['type']=1;
					$youke_id=M('users')->add($youke);
					session('user_id',$youke_id);
					session('user_name',$youke['username']);
					session('user_pic',$youke['user_pic']);
					session('login_time',$youke['login_time']);
					$this->success('登陆成功','/user/center');
				}		
			}else{
				
				$userinfo=M('users')->where("wb_openid='".$wb_openid."'")->find();
				if($userinfo){
					$uid=session('user_id');
					$user_info=M('users')->find($uid);
					if($userinfo['phone']&&$user_info['phone']){
						$this->error('该账户已被绑定，不可合并','/user/bduser');
					}else{
						$this->redirect('user/wb_qjbd',array('openid'=>$wb_openid));
					}
				}else{
					//直接绑定
					$user['wb_openid']=$wb_openid;
					$user['wb_name']=$user_message['screen_name'];
					$user['id']=session('user_id');
					M('users')->save($user);
					$this->redirect('user/bduser');
				}	
		}
		}
	}
	
	 public function qqlogin(){
		$appid=C('qq_login.QQ_APP_KEY');
		$scope='TencentLogin';
		$callback='https://m.58daiyan.com/user/qq_login';
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
			. $appid . "&redirect_uri=" . urlencode($callback)
			. "&state=" . $_SESSION['state']
			. "&scope=".$scope;
		header("Location:$login_url");
    }
	
	 public function qq_login(){
		$appid=C('qq_login.QQ_APP_KEY');
		$appkey=C('qq_login.QQ_APP_SECRET');
		$callback='http://m.58daiyan.com/user/qq_login';
		 $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
            . "client_id=" . $appid. "&redirect_uri=" . urlencode($callback)
            . "&client_secret=" . $appkey. "&code=" . $_REQUEST["code"];

        $response = file_get_contents($token_url);
        if (strpos($response, "callback") !== false)
        {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
            $msg = json_decode($response);
            if (isset($msg->error))
            {
                echo "<h3>error:</h3>" . $msg->error;
                echo "<h3>msg  :</h3>" . $msg->error_description;
                exit;
            }
        }else{
			$params = array();
			parse_str($response, $params);

			//debug
			//print_r($params);
			$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params["access_token"];

			$str  = file_get_contents($graph_url);
			if (strpos($str, "callback") !== false)
			{
				$lpos = strpos($str, "(");
				$rpos = strrpos($str, ")");
				$str  = substr($str, $lpos + 1, $rpos - $lpos -1);
			}

			$user = json_decode($str,true);
			
			
		
			if (isset($user['error']))
			{
				echo "<h3>error:</h3>" . $user['error'];
				echo "<h3>msg  :</h3>" . $user['error_description'];
				exit;
			}elseif(session('user_id')==null){
				$userinfo=M('users')->where("qq_openid='".$user["openid"]."'")->find();
				if(!$userinfo){
					$get_user_info = "https://graph.qq.com/user/get_user_info?"
						. "access_token=" . $params['access_token']
						. "&oauth_consumer_key=" . $appid
						. "&openid=" . $user["openid"]
						. "&format=json";

					$info = file_get_contents($get_user_info);
					$arr = json_decode($info, true);
					if($arr['nickname']){
						$youke['username']=$arr['nickname'];
						$youke['qq_name']=$arr['nickname'];
						$youke['password']="8888888";
						$youke['ishidden']=1;
						$youke['addtime']=time();
						$youke['user_pic']=$arr['figureurl_qq_2'];
						$youke['login_time']=date('Y-m-d H:i:s',time());
						$youke['sex']=0;
						$youke['qq_openid']=$user["openid"];
						$youke['type']=1;
						$youke_id=M('users')->add($youke);
						session('user_id',$youke_id);
						session('user_name',$youke['username']);
						session('user_pic',$youke['user_pic']);
						session('login_time',$youke['login_time']);
					}
				}else{
					//已绑定
					session('user_id',$userinfo['id']);
					session('user_name',$userinfo['username']);
					session('user_pic',$userinfo['user_pic']);
					session('login_time',$userinfo['login_time']);
					//更新登录时间
					$date_time=date('Y-m-d H:i:s', time());
					$login_info['id']=$userinfo['id'];
					$login_info['login_time']=$date_time;
					M('users')->save($login_info);
					
					//添加cookie 购物车
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						addcart($userinfo['id'],$v['shop_id'],$v['sku_id'],$v['num']);
					}
					cookie('cart_array',null);
				}
				$this->redirect('user/center');
			
				

				
			}else{
				//绑定
				$uid=session('user_id');
				
				$user_info=M('users')->find($uid);
				$user['qq_openid']=$user["openid"];
				$userinfo=M('users')->where("qq_openid='".$user["openid"]."'")->find();
				if($userinfo){
					if($userinfo['phone']&&$user_info['phone']){
						$this->error('该账户已被绑定，不可合并','/user/bduser');
					}else{
						$this->assign('openid',$user["openid"]);
						$this->display();
					}
				}else{
					$get_user_info = "https://graph.qq.com/user/get_user_info?"
						. "access_token=" . $params['access_token']
						. "&oauth_consumer_key=" . $appid
						. "&openid=" . $user["openid"]
						. "&format=json";

					$info = file_get_contents($get_user_info);
					$arr = json_decode($info, true);
					$user['qq_openid']=$user["openid"];
					$user['id']=session('user_id');
					$user['qq_name']=$arr['nickname'];
					M('users')->save($user);
					$this->redirect('user/bduser');
				}
			}
			
		
		}

        
    }
	
	public function qq_bd(){
		$appid=C('qq_login.QQ_APP_KEY');
		$scope='TencentLogin';
		$callback='https://m.58daiyan.com/user/qq_login';
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
			. $appid . "&redirect_uri=" . urlencode($callback)
			. "&state=" . $_SESSION['state']
			. "&scope=".$scope;
		header("Location:$login_url");
	}
	//qq绑定  并合并账号
	public function qq_qjbd(){
		$openid=I('post.openid');
		$uid=session('user_id');
		$is_bd=M('users')->where("qq_openid='".$openid."'")->find();
		if($is_bd){
			$user_info=M('users')->find($uid);
			if($is_bd['phone']&&$user_info['phone']){
				$msg['code']=0;
				$msg['msg']='该微信账户已被绑定，不可合并';
			}else{
				//qq合并
				$data['qq_name']=$is_bd['qq_name'];
				$data['qq_openid']=$openid;
				if($is_bd['phone']){
					if($is_bd['wb_openid']==null&&$user_info['wb_openid']){
						$data['wb_openid']=$user_info['wb_openid'];
						$data['wb_name']=$user_info['wb_name'];
					}
					if($is_bd['wx_openid']==null&&$user_info['wx_openid']){
						$data['wx_openid']=$user_info['wx_openid'];
						$data['wx_name']=$user_info['wx_name'];
						$data['wx_unionid']=$user_info['wx_unionid'];
					}
					$data['id']=$is_bd['id'];
					$deleid=$user_info['id'];
				}else{
					if($is_bd['wb_openid']&&$user_info['wb_openid']==null){
						$data['wb_openid']=$is_bd['wb_openid'];
						$data['wb_name']=$is_bd['wb_name'];
					}
					if($is_bd['wx_openid']&&$user_info['wx_openid']==null){
						$data['wx_openid']=$is_bd['wx_openid'];
						$data['wx_name']=$is_bd['wx_name'];
						$data['wx_unionid']=$is_bd['wx_unionid'];
					}
					$data['id']=$user_info['id'];
					$deleid=$is_bd['id'];
				}
				$rs=M('users')->save($data);
				if($rs){
					M('users')->delete($deleid);
					//更新登陆
					$new_user_info=M('users')->find($data['id']);
					session('user_id',$new_user_info['id']);
					session('user_name',$new_user_info['username']);
					session('user_pic',$new_user_info['user_pic']);
					session('login_time',$new_user_info['login_time']);
					$msg['code']=1;
					$msg['msg']='QQ绑定成功';
				}else{
					$msg['code']=0;
					$msg['msg']='QQ绑定失败';
				}
			}
			
		}else{
			$data['id']=$uid;
			$data['qq_openid']=$openid;
			$rs=M('users')->save($data);
			if($rs){
				$msg['code']=1;
				$msg['msg']='QQ绑定成功';
			}else{
				$msg['code']=0;
				$msg['msg']='QQ绑定失败';
			}
		}
		$this->ajaxReturn($msg);
	}
	
	function wb_qjbd(){
		if($_POST){
			$openid=I('post.openid');
			$uid=session('user_id');
			$is_bd=M('users')->where("wb_openid='".$openid."'")->find();
			if($is_bd){
				$user_info=M('users')->find($uid);
				if($is_bd['phone']&&$user_info['phone']){
					$msg['code']=0;
					$msg['msg']='该微博账户已被绑定，不可合并';
				}else{
					//qq合并
					$data['wb_name']=$is_bd['wb_name'];
					$data['wb_openid']=$openid;
					if($is_bd['phone']){
						if($is_bd['qq_openid']==null&&$user_info['qq_openid']){
							$data['qq_openid']=$user_info['qq_openid'];
							$data['qq_name']=$user_info['qq_name'];
						}
						if($is_bd['wx_openid']==null&&$user_info['wx_openid']){
							$data['wx_openid']=$user_info['wx_openid'];
							$data['wx_name']=$user_info['wx_name'];
							$data['wx_unionid']=$user_info['wx_unionid'];
						}
						$data['id']=$is_bd['id'];
						$deleid=$user_info['id'];
					}else{
						if($is_bd['qq_openid']&&$user_info['qq_openid']==null){
							$data['qq_openid']=$is_bd['qq_openid'];
							$data['qq_name']=$is_bd['qq_name'];
						}
						if($is_bd['wx_openid']&&$user_info['wx_openid']==null){
							$data['wx_openid']=$is_bd['wx_openid'];
							$data['wx_name']=$is_bd['wx_name'];
							$data['wx_unionid']=$is_bd['wx_unionid'];
						}
						$data['id']=$user_info['id'];
						$deleid=$is_bd['id'];
					}
					$rs=M('users')->save($data);
					if($rs){
						M('users')->delete($deleid);
						//更新登陆
						$new_user_info=M('users')->find($data['id']);
						session('user_id',$new_user_info['id']);
						session('user_name',$new_user_info['username']);
						session('user_pic',$new_user_info['user_pic']);
						session('login_time',$new_user_info['login_time']);
						$msg['code']=1;
						$msg['msg']='微博绑定成功';
					}else{
						$msg['code']=0;
						$msg['msg']='微博绑定失败';
					}
				}
				$this->ajaxReturn($msg);
			}
			
		}else{
			$openid=I('get.openid');
			$this->assign('openid',$openid);
			$this->display();
		}
		
	}
	
	
	public function wx_app_login(){
		
		if($_POST){
			$user_info=html_entity_decode(I('post.userInfo'));
			$weixin_info=json_decode($user_info,true);
			file_put_contents("app_login.txt", var_export($weixin_info,true));  
			$youke_info=M('users')->where("wx_unionid='".$weixin_info['unionid']."'")->find();
			if($youke_info){
				//存在自动登陆,不是主动注销
				session('user_id',$youke_info['id']);
			}elseif($zd_out!=1){
				//不存在创建游客账号,不是主动注销
				$youke['username']=$weixin_info['nickname'];
				$youke['password']="8888888";
				$youke['wx_name']=$weixin_info['nickname'];
				$youke['ishidden']=1;
				$youke['addtime']=time();
				$youke['user_pic']=$weixin_info['headimgurl'];
				$youke['login_time']=date('Y-m-d H:i:s',time());
				$youke['sex']=$weixin_info['sex'];
				$youke['wx_unionid']=$weixin_info['unionid'];
				$youke['type']=1;
				$youke_id=M('users')->add($youke);
				session('user_id',$youke_id);
			}
			if(session('user_id')){
				$perv_url='/user/center';
				$this->ajax_r(1,$perv_url,'登录成功！');
			}else{
				$this->ajax_r(0,'','登录失败！');
			}
			
		}
	}
    public function qq_app_login(){

        if($_POST){
            $user_info=html_entity_decode(I('post.userInfo'));

            $qq_info=json_decode($user_info,true);
            file_put_contents("app_login.txt", var_export($qq_info,true));
            $this->ajax_r(0,'','登录失败！');
            $youke_info=M('users')->where("qq_openid='".$qq_info['unionid']."'")->find();
            if($youke_info){
                //存在自动登陆,不是主动注销
                session('user_id',$youke_info['id']);
            }elseif($zd_out!=1){
                //不存在创建游客账号,不是主动注销
                $youke['username']=$weixin_info['nickname'];
                $youke['password']="8888888";
                $youke['wx_name']=$weixin_info['nickname'];
                $youke['ishidden']=1;
                $youke['addtime']=time();
                $youke['user_pic']=$weixin_info['headimgurl'];
                $youke['login_time']=date('Y-m-d H:i:s',time());
                $youke['sex']=$weixin_info['sex'];
                $youke['wx_unionid']=$weixin_info['unionid'];
                $youke['type']=1;
                $youke_id=M('users')->add($youke);
                session('user_id',$youke_id);
            }
            if(session('user_id')){
                $perv_url='/user/center';
                $this->ajax_r(1,$perv_url,'登录成功！');
            }else{
                $this->ajax_r(0,'','登录失败！');
            }

        }
    }
    public function wb_app_login(){

        if($_POST){
            $user_info=html_entity_decode(I('post.userInfo'));
            $weibo_info=json_decode($user_info,true);
            file_put_contents("app_login.txt", var_export($weibo_info,true));
            $youke_info=M('users')->where("wb_openid='".$weibo_info['openid']."'")->find();
            if($youke_info){
                //存在自动登陆,不是主动注销
                session('user_id',$youke_info['id']);
            }elseif($zd_out!=1){
                //不存在创建游客账号,不是主动注销
                $youke['username']=$weibo_info['nickname'];
                $youke['password']="8888888";
                $youke['wb_name']=$weibo_info['nickname'];
                $youke['ishidden']=1;
                $youke['addtime']=time();
                $youke['user_pic']=$weibo_info['headimgurl'];
                $youke['login_time']=date('Y-m-d H:i:s',time());
                $youke['sex']=$weibo_info['sex'];
                $youke['wb_openid']=$weibo_info['openid'];
                $youke['type']=1;
                $youke_id=M('users')->add($youke);
                session('user_id',$youke_id);
            }
            if(session('user_id')){
                $perv_url='/user/center';
                $this->ajax_r(1,$perv_url,'登录成功！');
            }else{
                $this->ajax_r(0,'','登录失败！');
            }

        }
    }
	
	public function app_qq_login(){
		
		$this->display();
		
	}
  public function help(){
		$term=M('article_terms')->where('parent=2')->select();
		foreach($term as $key =>$value){
			$count=M('article_posts')->where('trem_id="'.$value['term_id'].'"')->count();
			$term[$key]['count']=$count;
		}
	
		$this->assign('term',$term);
		$this->display();
	}
	public function help_list(){
		$term_id=I('get.id');
		$term=M('article_terms')->find($term_id);
		$this->assign('term',$term);
		$this->display();
	}
	public function ajax_help_list(){
		$term_id=I('get.id');
		$page=I('post.page');
		$amount=6;
		$count=M('article_posts')->where('trem_id="'.$term_id.'"')->count();
		$pages=ceil($count/$amount);
		$last=($page-1)*$amount;
		$list=M('article_posts')->where('trem_id="'.$term_id.'"')->limit($last,$amount)->select();
		$html='';
		foreach($list as $k=>$v){
			$html.=' <li class="mui-table-view-cell">
			        <a class="mui-navigate-right" href="'.U('user/help_info',array('id'=>$v['id'])).'">'.$v['post_title'].'</a>
			   </li>';
		}
		$rs['content']=$html;
		$rs['pages']=$pages;
		$this->ajaxReturn($rs);
	}
	function help_info(){
		$id=I('get.id');
		$info=M('article_posts')->find($id);
		$like_list=M('article_posts')->where('trem_id="'.$info['trem_id'].'" and id!='.$id)->order('rand()')->limit(3)->select();
	
		$this->assign('info',$info);
		$this->assign('like_list',$like_list);
		$this->display();
	}

	function ajax_getdaiyan_product(){
		$page=I('post.page');
		$amount=6;

		$uid=session('user_id');
    	$count=M('order_goods')
    		->field('yt_shop.title,yt_shop.image,yt_shop.price,yt_shop.redenvelope,yt_shop.id')
    		->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->join('yt_shop ON yt_order_goods.goods_id = yt_shop.id','left')
            ->where("(yt_order.order_state='40' and yt_order.refund_state=0 and yt_order.buyer_id=$uid) or (yt_order.order_state='20' and yt_order.refund_state=0 and yt_order.buyer_id=$uid and yt_order.order_type='2')")
            ->group('yt_order_goods.goods_id')
    		->count();
		$pages=ceil($count/$amount);
		$last=($page-1)*$amount;
    	$rs=M('order_goods')
    		->field('yt_shop.title,yt_shop.image,yt_shop.price,yt_shop.redenvelope,yt_shop.envelope_type,yt_shop.envelope_product_id,yt_shop.id')
    		->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->join('yt_shop ON yt_order_goods.goods_id = yt_shop.id','left')
            ->where("(yt_order.order_state='40' and yt_order.refund_state=0 and yt_order.buyer_id=$uid) or (yt_order.order_state='20' and yt_order.refund_state=0 and yt_order.buyer_id=$uid and yt_order.order_type='2')")
            ->group('yt_order_goods.goods_id')
			->limit($last,$amount)
    		->select();
    	$html_angel=M('angel')->where("uid=$uid")->select();
		$shop_html='';
        foreach($html_angel as $key=>$value){

            if($value['type']==2){ $store_html_status=true;}
            if($value['type']==3){$type_html_status=true;}
        }
        if($store_html_status){
            $store_html='<li style="float: left;width: 100%;font-size: 15px;font-weight: 800;padding: 5px 4px;background: #fff;margin-bottom: 1px;">天使代言店铺</li>';
		}
        if($type_html_status){
            $type_html='<ul style="padding: 5px; background: #fff;margin-bottom: 5px;float: left;width: 100%"><li style="float: left; width: 100%;font-size: 15px;font-weight: 800;padding: 5px 1px;">天使代言分类</li>';
        }


    	foreach($html_angel as $key=>$value){
    		
    		if($value['type']==1){
    			
				$angel_rs=M('shop')->find($value['type_id']);
				if($angel_rs['envelope_type']==0){
					$jiangli='<strong style="font-size: 12px;line-height: 20px;height:20px">红包：￥'.$angel_rs['redenvelope'].'</strong>';										
				}else{
					$envelope_product=get_shop_info($angel_rs['envelope_product_id']);
					$jiangli='<strong style="font-size: 12px;line-height: 20px;height:20px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">礼品：'.$envelope_product['title'].'</strong>';				
				}
				if($angel_rs){
                    $url=U('shop/index',array('id'=>$angel_rs['id'],'type'=>"daiyan"));
                    $shop_html.='<div class="single_item shouchang" id="more_element_2">
							<dl>
								<dt style="width: 29%;">
									<a href="'.$url.'">
										<img src="'.$angel_rs['image'].'" style="width: 80%;">
									</a>
								</dt>
								<dd style="width: 69%;">
									<a href="'.$url.'" class="title">
										<p><em style="text-decoration: initial;background: #f00;color: #fff;border-radius: 3px;padding: 1px 3px;">天使代言</em>&nbsp;'.$angel_rs['title'].'</p>
									</a>
									<span style="line-height: 7px;height:47px">
										<strong style="font-size: 12px;line-height: 20px;height:20px;color:#000;">售价：￥'.$angel_rs['price'].'</strong>
										'.$jiangli.'
										<a href="'.$url.'" class="zq_button" style="color: #fff">立即赚钱</a> 
									</span>
								</dd>
							</dl>
						</div>';
				}

			}
            if($value['type']==2){
                $angel_rs=M('store')->find($value['type_id']);
                if($angel_rs){
                    $angel_rs_content=M('store_template')->where("store_id=".$value['type_id'])->find();
                    $url=U('store/type',array('id'=>$angel_rs['id'],'types'=>"daiyan",'shop_id'=>0));
                    $store_html.='<div class="single_item shouchang" id="more_element_2">
							<dl style="margin-bottom: 1px;">
								<dt style="width: 29%;">
									<a href="'.$url.'">
										<img src="'.$angel_rs_content['portrait'].'" style="width: 80%;">
									</a>
								</dt>
								<dd style="width: 69%;">
									<a href="'.$url.'" class="title">
										<p><em style="text-decoration: initial;background: #f00;color: #fff;border-radius: 3px;padding: 1px 3px;">天使代言</em>&nbsp;'.$angel_rs['title'].'(店铺)</p>
									</a>
									<span style="line-height: 7px;height:20px">
									
										<a href="'.$url.'" class="zq_button" style="color: #fff">立即赚钱</a> 
									</span>
								</dd>
							</dl>
						</div>';
                }
            }
            if($value['type']==3){
                $angel_rs=M('ticket_type')->find($value['type_id']);
                if($angel_rs){
                    $url=U('list/index',array('id'=>$angel_rs['id'],'types'=>"daiyan"));
                    $type_html.='<li style="background: #fbca60;padding: 5px;float: left;margin: 4px 4px 0 0;"><a href="'.$url.'" style="color: #fff;">'.$angel_rs['name'].'</a></li>';
                }
            }
		}

        $type_html.='</ul>';

        $html='';
        if($shop_html&&$rs){
        	$cp_header='<li style="float: left;width: 100%;font-size: 15px;font-weight: 800;padding: 5px 4px;background: #fff; margin: 4px 0 1px 0;">产品代言</li>';
        }
        $html.=$type_html.$store_html.$cp_header.$shop_html;

		foreach($rs as $k=>$v){
			if($v['envelope_type']==0){
					$jiangli='<strong style="font-size: 12px;line-height: 20px;height:20px">红包：￥'.$v['redenvelope'].'</strong>'	;									
				}else{
					$envelope_product=get_shop_info($v['envelope_product_id']);
					$jiangli='<strong style="font-size: 12px;line-height: 20px;height:20px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">礼品：'.$envelope_product['title'].'</strong>';				
				}
			$url=U('shop/index',array('id'=>$v['id'],'type'=>"daiyan"));
				$html.='<div class="single_item shouchang" id="more_element_2">
				<dl>
					<dt style="width: 29%;">
						<a href="'.$url.'">
							<img src="'.$v['image'].'" style="width: 80%;">
						</a>
					</dt>
					<dd style="width: 69%;">
						<a href="'.$url.'" class="title">
							<p>'.$v['title'].'</p>
							
						</a>
						<span style="line-height: 7px;height:47px">
							<strong style="font-size: 12px;line-height: 20px;height:20px;color:#000;">售价：￥'.$v['price'].'</strong>
							'.$jiangli.'
							<a href="'.$url.'" class="zq_button" style="color: #fff">立即赚钱</a> 
						</span>
					</dd>
				</dl>
			</div>';
		}
    	$rs['content']=$html;
		$rs['pages']=$pages;
		$this->ajaxReturn($rs);
	}
    public function ajax_getcrowdfunding_list(){
        $order=M('order');
        $uid=session('user_id');
        $page=I('post.page');
        $amount=6;
        $where['buyer_id']=$uid;
        if(I('post.stauts')==1){
            //已支付
            $where="(yt_order.order_state='20' or yt_order.order_state='30'or yt_order.order_state='40')";
        }
        if(I('post.stauts')==2){
            //未付款
            $where="yt_order.order_state='10'";
        }
        $last=($page-1)*$amount;
        $count=$order
            ->field("yt_order.*,yt_order_goods.goods_id,yt_order_goods.goods_name,yt_order_goods.goods_price,yt_order_goods.goods_image")
            ->join('yt_order_goods ON yt_order.order_id = yt_order_goods.order_id','left')
            ->where("yt_order.buyer_id=$uid and yt_order_goods.goods_id in (SELECT goods_id from yt_crowdfunding) and ".$where)
            ->count();
        $pages=ceil($count/$amount);
        $last=($page-1)*$amount;
        $order_list=$order
            ->field("yt_order.*,yt_order_goods.goods_id,yt_order_goods.goods_name,yt_order_goods.goods_price,yt_order_goods.goods_image")
            ->join('yt_order_goods ON yt_order.order_id = yt_order_goods.order_id','left')
            ->where("yt_order.buyer_id=$uid and yt_order_goods.goods_id in (SELECT goods_id from yt_crowdfunding) and ".$where)
            ->limit($last,$amount)
            ->order('yt_order.order_id desc')
            ->select();
        //print_r($order->getLastSql());
        /*$order_list=$order->where($where)->limit($last,$amount)->order('order_id desc')->select();
        $count=$order->where($where)->count();
        $pages=ceil($count/$amount);*/
        foreach($order_list as $key=>$value){
            if($value['tuanselect']==0){
                if($value['refund_state']==0){
                    if($value['order_state']=='0'){
                        $top_info='已取消';
                        $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment">已取消</a>
									</div>
								</div>';
                    }
                    if($value['order_state']=='10'){
                        $top_info='等待付款';
                        $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									<div class="order-bottom-btn">
										<a class="cancel-payment edit-order" data-id="'.$value['order_id'].'" data-type="close_order" >取消订单</a>
									</div>
		
									<div class="order-bottom-btn">
										<a href="'.U('pay/paychoose',array('order_cn'=>$value['order_sn'])).'" class="on-payment cur">立即付款</a>
									</div>
									<!-- 确认收货  -->
								</div>';
                    }
                    if($value['order_state']=='20'){
                        $top_info='已付款，待发货';
                        $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
									</div>
									<div class="order-bottom-btn">
										<a class="on-payment cur" >等待发货</a>
									</div>
								</div>';
                    }
                    if($value['order_state']=='30'){
                        $top_info='已发货，待收货';
                        $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
									</div>
									<div class="order-bottom-btn">
										<a class="on-payment cur edit-order"  data-id="'.$value['order_id'].'" data-type="queren_get">确认收货</a>
									</div>
								</div>';
                    }
                    if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
                        $top_info='已收货，待评价';
                        $endtime=$value['finnshed_time']+60*60*24*7;
                        $nowtime=time();
                        if($nowtime<$endtime){
                            $tuihuo='<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
								</div>';
                        }
                        $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									'.$tuihuo.'<div class="order-bottom-btn">
										<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
									</div>
								</div>';
                    }
                }else{
                    if($value['refund_state']==1){
                        $top_info='退款中';
                        $button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">退款中</a>
									</div>
								</div>';
                    }
                    if($value['refund_state']==2){
                        $top_info='同意退款';
                        $button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">同意退款</a>
									</div>
								</div>';
                    }
                    if($value['refund_state']==3){
                        $top_info='拒绝退款';
                        $button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">拒绝退款</a>
									</div>
								</div>';
                    }
                    if($value['refund_state']==4){
                        $top_info='等待财务待打款';
                        $button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">待打款</a>
									</div>
								</div>';
                    }
                    if($value['refund_state']==5){
                        $top_info='退款完成';
                        $button='<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment" href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'">退款完成</a>
									</div>
								</div>';
                    }
                    if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
                        $top_info='已收货，待评价';
                        $endtime=$value['finnshed_time']+60*60*24*7;
                        $nowtime=time();
                        if($nowtime<$endtime){
                            $tuihuo='<div class="order-bottom-btn">
										<a href="'.U('user/refunddetail',array('order_id'=>$value['order_id'])).'" class="on-payment " >退款/退货</a>
								</div>';
                        }
                        $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<!-- 取消订单 -->
									'.$tuihuo.'<div class="order-bottom-btn">
										<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
									</div>
								</div>';
                    }
                }
            }else{
                //已取消团购订单
                if($value['order_state']=='0'){
                    $top_info='已取消';
                    $button='<!-- 操作 -->
								<div class="order-bottom-con ">
									<div class="order-bottom-btn">
										<a class="cancel-payment">已取消</a>
									</div>
								</div>';
                }
                if($value['refund_state']=='0'){
                    if($value['order_state']=='10'){
                        $top_info='等待付款';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="cancel-payment edit-order" data-id="'.$value['order_id'].'" data-type="close_order" >取消订单</a>
										</div>
			
										<div class="order-bottom-btn">
											<a href="'.U('pay/paychoose',array('order_cn'=>$value['order_sn'])).'" class="on-payment cur">立即付款</a>
										</div>
										<!-- 确认收货  -->
									</div>';
                    }
                    if($value['order_state']=='20'){
                        if($value['tuanselect']==1){
                            if($value['tuan_is_first_id']==0){
                                $num=get_tuan($value['order_id']);
                            }else{
                                $num=get_tuan($value['tuan_is_first_id']);
                            }
                            if($num<$value['tuannum']){
                                $tuan_type='拼团中';
                            }else{
                                $tuan_type='拼团成功';
                            }

                        }
                        $top_info='已付款，待发货';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur" >'.$tuan_type.'</a>
										</div>
									</div>';
                    }
                    if($value['order_state']=='30'){
                        $top_info='已发货，待收货';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur edit-order"  data-id="'.$value['order_id'].'" data-type="queren_get">确认收货</a>
										</div>
									</div>';
                    }
                    if($value['order_state']=='40'&&$value['evaluation_state']=='0'){
                        $top_info='已收货，待评价';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur edit-order" data-id="'.$value['order_id'].'" data-type="evaluation">立即评价</a>
										</div>
									</div>';
                    }
                }else{
                    if($value['refund_state']=='1'){
                        $top_info='退款中';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">退款中</a>
										</div>
									</div>';
                    }
                    if($value['refund_state']=='2'){
                        $top_info='同意退换货';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">同意退换货</a>
										</div>
									</div>';
                    }
                    if($value['refund_state']=='3'){
                        $top_info='商家拒绝';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">商家拒绝</a>
										</div>
									</div>';
                    }
                    if($value['refund_state']=='4'){
                        $top_info='退款/货完成';
                        $button='<!-- 操作 -->
									<div class="order-bottom-con ">
										<!-- 取消订单 -->
										<div class="order-bottom-btn">
											<a class="on-payment cur" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">退款/货完成</a>
										</div>
									</div>';
                    }
                }
            }
            if($value['shipping_fee']!=0){
                $value['shipping_fee']='运费：￥'.$value['shipping_fee'];
            }else{
                $value['shipping_fee']='免邮';
            }
            $shop_list=get_order_shop($value['order_id']);

            $order_list='<div class="order-list" id="order_list_'.$value['order_id'].'">
						<h2>
							<a href="'.U('store/index',array('id'=>$value['store_id'])).'">
								<i class="shop-icon"></i>
								<span>'.$value['store_name'].'</span>
							</a>
							<strong class="color">'.$top_info.'</strong>
						</h2>
						<ul class="order-list-goods">';
            foreach($shop_list as $k=>$v){
                $order_list.='<li>
					<a data="'.U('shop/index',array('id'=>$v['goods_id'])).'" href="'.U('user/order_info',array('id'=>$value['order_id'])).'">
						<div class="goods-pic">
							<img src="'.$v['goods_image'].'" />
						</div>
						<dl class="goods-info">
							<dt class="goods-name"> '.$v['goods_name'].'</dt>
							<dd class="goods-attr">';
                if($v['goods_sku_spec']){
                    $order_list.='<span>规格&nbsp;:&nbsp;'.$v['goods_sku_spec'].'</span>&nbsp;&nbsp;';
                }
                if($v['goods_sku_style']){
                    $order_list.='<span>颜色&nbsp;:&nbsp;'.$v['goods_sku_style'].'</span>';
                }

                $order_list.='</dd>
							<dd class="goods-price price-color">
								<em>￥'.$v['goods_price'].'</em>
								<span class="goods-num">x'.$v['goods_num'].'</span>
							</dd>
						</dl>
					</a>
				</li>';
            }
            $youhui_price=get_coupon_price($value['buyer_id'],$value['coupon']);
            $order_amount=$value['order_amount']-$value['daiyan_sale']-$youhui_price;
            if($order_amount<0){
                $order_amount=0.00;
            }
            $count_num=M('order_goods')->where("order_id=".$value['order_id'])->sum('goods_num');
            $order_list.='</ul>
							<div class="price">
								<span>共'.$count_num.'件商品&nbsp;&nbsp;实付：</span>
								<span class="price_sum price-color">¥<strong>'.$order_amount.'</strong></span>
								<span class="freight">('.$value['shipping_fee'].')</span>
							</div>
	
							<!-- 操作 -->
							<div class="order-bottom-con ">'.$button.'</div></div>
						';
            $res[]=$order_list;
        }
        $content='';
        foreach($res as $key=>$val){
            $content=$content.$val;
        }
        $rs['content']=$content;
        $rs['pages']=$pages;
        $this->ajaxReturn($rs);
    }
}
?>
